I have a code looking like this :
struct point {
int a;
int b;
}
which can be further used like this :
struct point p;
p.x = 10;
p.y = 5;
now I came to know that this can also be written like this :
typedef struct{
int x;
int y;
} point;
and can be used as point p
The confusion started when I started learning linked-list, this is the code I saw.
typedef struct node {
int val;
struct node * next;
} node_t;
I have a couple of questions:
typedef struct { ... } node whats the use of writing typedef struct node {.....node_t at the end of the code is really confusing because from my understanding it has already defined a type node so we can call node x to create a node , then what's the need of the node_t or basically writing anything after the } what it means ?shouldn't this work ?
typedef struct {
int val;
struct node * next;
} node;
If we can define the struct by simply using
typedef struct { ... } nodewhats the use of writingtypedef struct node {.....The
node_tat the end of the code is really confusing because from my understanding it has already defined a typenodeso we can callnode xto create a node , then what's the need of thenode_tor basically writing anything after the}what it means ?
In C, structures can have both tag and a typedef name. In the structure declaration:
typedef struct node {
int val;
struct node * next;
} node_t;
node is a tag and node_t is typedef name. Latter you can declare your structure variable either by using tag or typedef name.
struct node *new_node; // OK
node_t *head; // OK
In fact, the tag and typedef name can even be same, although that's not required:
typedef struct node {
int val;
struct node * next;
} node;
shouldn't this work ?
typedef struct {
int val;
struct node * next;
} node;
No. This will not work . Why?
The reason is that, when a structure has a member that points to the same kind of structure, as node does, we are required to use a structure tag. Without the node tag, we would have no way to declare the type of next.
Suggested reading: As Op is asking for good resource on data structure. Here you can go:
The third block of code, which you seem to have a question about, has a redundant identifier.
typedef struct node {
int val;
struct node * next;
} node_t;
is identical to
struct node{
int val;
struct node * next;
};
typedef struct node node_t;
They're identical, but merged into the same line. The syntax around a typedef is
typedef [original name] [new name];
Normally, when we typedef a struct, the ONLY way to reference it afterwards would be the typedeffed name (in the first code example, you could only access it by node_t) while a slightly more redundant declaration allows for accessing by the "struct node" variable type too.
To answer your other question the node_t that confused you is what you can refer to the struct as... for example
node_t node1;
struct node node2;
Are the two valid declarations for a node struct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With