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:
- 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 ?
shouldn't this work ?
typedef struct {
int val;
struct node * next;
} node;
node xbecausenodeis a tag name.;). The class name is a typename without the need for atypedefalias. But, despite the C++ tag, it looks like you're just asking about C.