Why is there structure in the same structure ? What does that mean ? And can you give me an Example? Thanks.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is there structure in the same structure ? What does that mean ? And can you give me an Example? Thanks..

Struct Element { Struct Element *pointer; };

24th Jan 2018, 4:58 PM
Youssif Almassri
Youssif Almassri - avatar
2 Answers
+ 2
You use this for linked lists, so you can make an item point to the next item in the list. struct list { int i; /* and whatever other data you need */ struct list* next; }; You can then connect elements of this type in a chain: struct list a = {1, NULL}; struct list b = {2, &a}; struct list head = {3, &b}; Now your data is chained as: 3 - 2 - 1: head.next is a pointer to b, so head.next->i gives 2 head.next->next is a pointer to a, so head.next->next->i gives 1 You can also loop through your list: struct list* p = &head; while (p != NULL) { printf("value: %d\n", p->i); p = p->next; } (Please keep in mind: all code was simply typed in without compile or check if it actually works) Do have a look at the wikipedia page about linked lists: https://en.wikipedia.org/wiki/Linked_list
25th Jan 2018, 10:09 PM
Freddy
+ 1
Here is a nice Sololearn article about linked lists: https://www.sololearn.com/learn/634/?ref=app
26th Jan 2018, 7:13 AM
Freddy