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?

10
  • Self-referencing pointers are impossible in C since the types would be incompatible: int * p = &p; type of &p is int **.
    – Kninnug
    Aug 25, 2016 at 17:21
  • 1
    What does your C-book say? What specifically can't you deduce from what you should have learned at that point from struct definitions and pointers? In case you did: don't skip lessons! Aug 25, 2016 at 17:22
  • 1
    stackoverflow.com/questions/18451788/…
    – Arun
    Aug 25, 2016 at 17:23
  • But i found this in structure.
    – Akash Bera
    Aug 25, 2016 at 17:23
  • 1
    The only self-referential pointer would be a void *. There legally cannot be other self-referential pointers. (Is there any language which allows that and it is useful?) Aug 25, 2016 at 17:28

3 Answers 3

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.

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.

0
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.

4
  • 1
    The only self-referential pointer would be a void *. There legally cannot be other self-referntial pointers! Aug 25, 2016 at 17:27
  • 1
    yes brother, you can go through stackoverflow.com/questions/588623/…
    – Vikrant
    Aug 25, 2016 at 17:32
  • 1
    @Olaf, its not literally self-referential; but it avoids recursion with referencing itself. so clever ! :)
    – Vikrant
    Aug 25, 2016 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) Aug 25, 2016 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.