1

It is not allowed to declare a variable of same structure type but it is allowed to declare a pointer. For example,

struct node {
     int a;
     struct node x;    //not allowed
     struct node *pnext;   //allowed
     };

My question is when we can declare a pointer of same type why not a variable of same type.
I will be exremely grateful if somebody answers my question.

Thanks
Iqbal

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by Iqbal_h_a
1

Because if you are allowed to do so, creating one variable of that type will go on creating nested variables inside it and eat all available memory.

4

You've basically written this

struct node {
  int a;
  struct node {
    int a;
      struct node {
        int a;
        struct node {
          int a;
          struct node {
              int a;
              struct node x;  // and so on....
              struct node *pnext;
          }x;
          struct node *pnext;
        }x;
        struct node *pnext;
      }x;
    struct node *pnext;
  }x;
  struct node *pnext;
};

You can't infinitely expand copies of yourself when declaring a structure.

The way the cycle is broken is by having a pointer, which is always a fixed size regardless of the amount of data being pointed at.

Votes + Comments
nice explanation (andor)
Nicely Explained - [Grunt]
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.