0
struct LinkedList  
{  
    int data;
    struct LinkedList *next;
};

In the code, within the definition of struct LinkedList there is a pointer to the structure itself.

How does it work?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Akash Bera
  • 53
  • 1
  • 8

3 Answers3

7

So, the code

struct LinkedList  
{  
    int data;
    struct LinkedList *next;
};

defines a struct type containing two members named data and next, with the next member storing the address of a different object of the same type. Given the code:

struct LinkedList Node1 = { .data = 1, .next = NULL };
struct LinkedList Node0 = { .data = 0, .next = &Node1 };

you get something that sort of looks like this:

Node0              Node1
+---+--------+    +---+------+
| 0 | &Node1 |--->| 1 | NULL |
+---+--------+    +---+------+

(Note that you would never create a linked list this way, this is just for illustration).

This is possible for two reasons:

  1. C allows you to declare pointers to incomplete types;
  2. Pointers to struct types all have the same size and representation.

This is an example of a self-referential data type, which simply means that the type stores a reference (pointer) to a different object of the same type.

John Bode
  • 119,563
  • 19
  • 122
  • 198
4

What you talk about are recursive data structrues and the question is how to let a data structure reference itself.

In C this can be done by declaring a pointer to itself in the definition of the data structure, "self" meaning a thing of its own type.

Note that when you write the expression, the data structure is not yet complete. Therefore it is not possible to let a data structue contain an occurence of itself, for once because the definition is not yet completely known and for two because the data strutcure would be never ending containing an occurrence of itself, itself,...

But you can declare a pointer to itself. The compiler now only allocates storage for the pointer and if you later assign/dereference the pointer it knows the storage pointed to contains an occurrence of itself. That is what you do in your example.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
2

A self-referential pointer which points to the address of whatever it is a part of. So for example,

typedef struct node {     
   char data[30]; 
   struct node *this; 
   struct node *next; 
} Node; 

*this is a self-referential pointer if it is assigned to whatever is applied to.

,and

Clearly a Cell cannot contain another cell as it becomes a never-ending recursion.

However a Cell CAN contain a pointer to another cell.

Refer this post as well.

Community
  • 1
  • 1
Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • 1
    The only self-referential pointer would be a `void *`. There legally cannot be other self-referntial pointers! – too honest for this site Aug 25 '16 at 17:27
  • 1
    yes brother, you can go through http://stackoverflow.com/questions/588623/self-referential-struct-definition – Vikrant Aug 25 '16 at 17:32
  • 1
    @Olaf, its not literally self-referential; but it avoids recursion with referencing itself. so clever ! :) – Vikrant Aug 25 '16 at 17:33
  • It is common practice since the early beginning if linked data structures. Not new with C. But what is different here from any other `struct xy *`? Nothing! It does not make any difference where a pointer is declared. But maybe: "Any kind of higher techique may looklike magic to the unaware" (my translation, finder may keep any flaws) – too honest for this site Aug 25 '16 at 17:39