Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   about structure (http://www.go4expert.com/forums/about-structure-t21395/)

ravindrareddy3 17Mar2010 19:06

about structure
 
why we will not create a variable as a datamember in structure?
for eg:
struct some
{
int a;
float g;
struct some n;
};

itstimetojazz 18Mar2010 11:34

Re: about structure
 
When u create a structure, a definition is being created. But when u insert a variable of the same structure in the definition, the structure is still uninitialised. Hence the compiler cannot recognize the prototype of the variable. and it throws an error.

xpi0t0s 18Mar2010 14:28

Re: about structure
 
A structure is the definition of a new datatype, not the definition of a new variable.
But you can do both in a single step:
struct some
{
int a;
float g;
struct some *n;
} mysome;

to define mysome as the above unnamed structure (which means you can't use that structure anywhere else).

By the way, "struct some n" won't work within the definition of struct some; you have to use pointers, because at that point "struct some" isn't fully defined, so it doesn't know how much space to allocate. But it knows how big a pointer is so that's no problem.


All times are GMT +5.5. The time now is 12:41.