CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

C--Structures(inside structure declaration of variable of same type is not allowed) topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2006, 10:26 AM
Junior Member
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Iqbal_h_a is on a distinguished road
| More
C--Structures(inside structure declaration of variable of same type is not allowed)

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

Code:
    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
Reply With Quote
  #2  
Old 09-10-2006, 10:30 AM
Junior Member
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Iqbal_h_a is on a distinguished road
| More
Re: C--Structures(inside structure declaration of variable of same type is not allowed)

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

Code:
    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
Reply With Quote
  #3  
Old 09-10-2006, 10:34 AM
Junior Member
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Iqbal_h_a is on a distinguished road
| More
Re: C--Structures(inside structure declaration of variable of same type is not allowed)

Sorry I probably clicked the submit button more than once.
Reply With Quote
  #4  
Old 12-26-2006, 04:36 AM
Junior Member
 
Join Date: Dec 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Aztec is on a distinguished road
| More
Re: C--Structures(inside structure declaration of variable of same type is not allowed)

Quote:
Originally Posted by Iqbal_h_a View Post
It is not allowed to declare a variable of same structure type
because structure is not yet defined.
Reply With Quote
  #5  
Old 06-05-2007, 08:34 PM
Junior Member
 
Join Date: Jun 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
tiger12506 is on a distinguished road
| More
Re: C--Structures(inside structure declaration of variable of same type is not allowe

There is a very very good reason for this.
If the variable isn't defined yet, can't you declare it first and then define it? (one might ask)

Code:
struct node {
     int a;
     struct node x;    //not allowed
  };
This is how I see it. Follow in your mind what the compiler would do when you declare a variable of type struct node

1) It sees it has to allocate space for an integer.
2) It sees it has to allocate space for a struct node
3) It looks at the definition of struct node and then...
4) Repeats steps 1 - 3 indefinitely

If this were not a compiler error, then the compiler would run until the computer crashed.

But! You can declare a pointer to the type inside the struct. Follow what happens in that case

1) It sees it has to allocate space for an integer.
2) It sees it has to allocate space for a pointer (Constant between all types - typically 4 bytes)
3) Sets the type of the pointer to point to a struct node (so that it knows how to increment the pointer if necessary)
4) Variable definition succeeds!

Perhaps it is well to know *why* things are the way they are.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structure Padding imaginative.bibhas C Programming 9 12-23-2008 03:50 AM
Incrementing variable within an equality/divisibility test che3ver C++ Programming 1 07-18-2008 05:31 AM
Problem in choosing data structure tonyaim83 C Programming 2 06-29-2008 09:07 AM
Problem with Variables declaration whirlwind C# Programming 0 04-12-2008 10:13 AM
changing variable types of a function of a derived class mrclash C# Programming 0 09-20-2006 04:56 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 01:56 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board