User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,488 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,418 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2405 | Replies: 3
Reply
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Question C--Structures(inside structure declaration of variable of same type is not allowed)

  #1  
Sep 6th, 2006
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
Last edited by WolfPack : Sep 6th, 2006 at 5:02 am. Reason: Moved the question to outside code tags.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: C--Structures(inside structure declaration of variable of same type is not allowed)

  #2  
Sep 6th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Dec 2005
Posts: 3,640
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: C--Structures(inside structure declaration of variable of same type is not allowe

  #3  
Sep 6th, 2006
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: C--Structures(inside structure declaration of variable of same type is not allowe

  #4  
Sep 7th, 2006
Thank you very much for the kind reply.
Last edited by WolfPack : Sep 7th, 2006 at 1:20 am. Reason: Removed the code tags from the answer. Use code tags only when you are posting code.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC