Absolutely valid. Usually, you can take full advantage of this way by defining two types together:
typedef struct
{
int a;
int b;
} S1, *S1PTR;
Where S1 is a struct and S1PTR is the pointer to this struct.
Answer from alexkr on Stack OverflowAbsolutely valid. Usually, you can take full advantage of this way by defining two types together:
typedef struct
{
int a;
int b;
} S1, *S1PTR;
Where S1 is a struct and S1PTR is the pointer to this struct.
Answer from alexkr on Stack OverflowAbsolutely valid. Usually, you can take full advantage of this way by defining two types together:
typedef struct
{
int a;
int b;
} S1, *S1PTR;
Where S1 is a struct and S1PTR is the pointer to this struct.
Yes it is. But it is in my opininon bad style. Not the direct declaration of the struct, but the direct declaration of a pointer type. It is obfuscation, the information that a given variable or parameter is a pointer (and to a lesser extent for arrays) is extremely important when you want to read code.
When reviewing code it is often difficult to see at first glance which function could have a side effect or not. If the types used hide this information, it adds a memorisation burden to the reader.
int do_fancy(vector a, vector b);
or
int do_fancy(vector *a, vector *b);
in the first case I can miss easily that that function may change the content of a or b. In the second I'm warned.
And when actually writing code I also know directly to write a->x and not have the compiler tell me error: request for member x' in something not a structure or union`.
I know, it looks like a personal taste thing, but having worked with a lot of external code, I can assure you that it's extremely annoying when you do not recognize the indirection level of variables. That's one reason I also dislike C++ references
(in Java it's not because all objects are passed by reference, it's consistent) and Microsoft's LPCSTR kind of types.
Typedef Struct Pointer
Typedef struct with pointer - C++ Forum
c - typedef struct pointer definition - Stack Overflow
[deleted by user]
Videos
You cannot use pEDGE within the definition of the struct. You shoud do something like:
typedef struct edge {
pEDGE_ITEM *edge_item;
struct edge *next; //pointer to next edge
struct edge *prev; //pointer to prev edge
} EDGE, *pEDGE;
You must also note that edge_item is a double pointer. You also mention that in your question. So if you use pEDGE_ITEM and you just want to have a normal pointer you should not write pEDGE_ITEM *edge_item but just pEDGE_ITEM edge_item.
For clarifying, all the following declarations are equivalent:
struct edgeitem *edge_item;
EDGE_ITEM *edge_item;
pEDGE_ITEM edge_item;
But
pEDGE_ITEM *edge_item;
is equivalent to
struct edgeitem **edge_item;
EDGE_ITEM **edge_item;
About *EDGE next that is wrong syntax. The correct syntax would be EDGE* next or pEDGE next. So once struct edge is defined you can just use any of these two, but while defining the struct, you must do as I show at the beginning of my answer.
Yes, the following two definitions are equivalent:
typedef struct edge_list {
EDGE *head;
} EDGE_LIST;
typedef struct edge_list {
pEDGE head;
} EDGE_LIST;
You are using a type alias before it is defined. Using the structure tag is a workaround:
typedef struct edge
{
EDGE_ITEM *edge_item;
struct edge *next; //pointer to next edge
struct edge *prev; //pointer to prev edge
} EDGE;
Note that you almost surely used the pointer aliases incorrectly, I avoided them.
You can typedef them at the same time:
typedef struct Node {
int data;
struct Node *nextptr;
} node, *node_ptr;
This is arguably hard to understand, but it has a lot to do with why C's declaration syntax works the way it does (i.e. why int* foo, bar; declares bar to be an int rather than an int*
Or you can build on your existing typedef:
typedef struct Node {
int data;
struct Node *nextptr;
} node;
typedef node* node_ptr;
Or you can do it from scratch, the same way that you'd typedef anything else:
typedef struct Node* node_ptr;
To my taste, the easiest and clearest way is to do forward declarations of the struct and typedef to the struct and the pointer:
typedef struct node node;
typedef node * node_ptr;
struct node {
int data;
node_ptr nextptr;
};
Though I'd say that I don't like pointer typedef too much.
Using the same name as typedef and struct tag in the forward declaration make things clearer and eases the API compability with C++.
Also you should be clearer with the names of your types, of whether or not they represent one node or a set of nodes.
You need to do it in this order:
typedef struct Node Node;
struct Node
{
int value;
Node *next;
Node *prev;
};
That doesn't do exactly what you asked, but it solves the problem and is how this generally is done. I don't think there's a better way.
This kind of forward declaration has a second usage, in data hiding. If the list was implemented in a library, you could have just the typedef in the public header, along with functions like:
Node * list_new(void);
Node * list_append(Node *head, Node *new_tail);
size_t list_length(const Node *head);
This way, users of the library don't have easy access to the internals of your library, i.e. the fields of the Node structure.
Another acceptable way and with the least change to OP's code is the following:
typedef struct NodeT {
int value;
struct NodeT * next;
struct NodeT * prev;
} Node;
Note the introduction of NodeT and its usage in next and prev until Node is available.