Reputation: 21
can anybody help me understand what this block of code means?
typedef struct node{
int data;
struct node* next;
}test;
From my understanding, typedef is just a way to save characters instead of typing 'struct node' every time, which we can do by typing 'test' in this case. However, how can there be another 'struct node* next' inside struct node? I thought structs had to have structure members, so what does 'struct node* next' accomplish in this code? 'struct node* next' has no structure members, so what is the point of writing struct?
Thank you.
Upvotes: 1
Views: 533
Reputation: 10430
how can there be another 'struct node* next' inside struct node?
struct node *
is a pointer to struct node
user-defined data type. You can call it pointer to itself
. A struct can have pointers to itself or other structs as members.
The C for Programmers a Deitel book
says:
A structure cannot contain an instance of itself. For example, a variable of type struct employee cannot be declared in the definition for struct employee. A pointer to struct employee, however, may be included.
To read more on why a struct can't contain an instance of itself, you can go through this link.
typedef is just a way to save characters instead of typing 'struct node' every time, which we can do by typing 'test' in this case.
Without the forward declaration the struct name is still valid inside the struct definition (i.e. you can used struct A), but the typedef is not available until after the typedef definition is complete. You can read more over here.
I thought structs had to have structure members, so what does 'struct node* next' accomplish in this code? 'struct node* next' has no structure members, so what is the point of writing struct?
You can access the members of pointer to struct node by using ->
operator. Consider a small linked link program:
test a;
test b;
a.data = 5;
b.data = 10;
b.next = NULL;
a.next = &b; // A linked list has been formed.
printf("Data of first node is %d and second node is %d\r\n", a.data, a.next->data); // It will print "Data of first node is 5 and second node is 10"
Upvotes: 0
Reputation: 958
It's just a pointer to another node
. The reason you need the struct
in front is because the typedef
isn't valid until after the struct has been declared.
You might use that pointer to create a linked list, in which each node
contains a pointer to the next node
in the list.
This answer goes into more detail about how (and why) you might set things up like this. It also goes into more detail about why it's necessary (in your example) to use the struct
identifier in front of the node
pointer.
You may also want to think about whether or not you want to typedef your struct. It saves keystrokes, but it also hides the fact that it's a user-defined type. This answer goes into more detail about that.
Upvotes: 1
Reputation: 2291
Its common that structures are combined to form linked lists and trees etc. This is achieved by including a pointer to the children or parent "nodes. In your case is pointing to the next structure in the list.
Upvotes: 0