EmbLogic's Blog

Inside structure declaration of variable of same type is not allowed

 struct node {
     struct node x;    //not allowed
     struct node *head;   //allowed
     };

Why does we can declare a pointer of same type but not a variable of same type?

One Response to Inside structure declaration of variable of same type is not allowed

  1. Ankit.e1 says:

    See it this way.. When we declare a pointer it tells the compiler to allocate space for the pointer, which is of structure type in your case it’s self referential.

    But if you take a variable same as the structure it will have an endless chain. Let The struct that you have declared be allocated memory. Then that space will be having a pointer and a variable of the same struct type be it x. But x must also be having a pointer and a stuct variable of its type, thus it will create an endless chain…. Hence tis type of declaration is only imaginary :)

Leave a Reply to Ankit.e1 Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>