about structure

Newbie Member
17Mar2010,19:06   #1
ravindrareddy3's Avatar
why we will not create a variable as a datamember in structure?
for eg:
struct some
{
int a;
float g;
struct some n;
};
Light Poster
18Mar2010,11:34   #2
itstimetojazz's Avatar
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.
Mentor
18Mar2010,14:28   #3
xpi0t0s's Avatar
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.