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

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.

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.

commented: Nicely Explained - [Grunt] +1
commented: nice explanation (andor) +2

Thank you very much for the kind reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.