about structure

ravindrareddy3's Avatar, Join Date: Mar 2010
Newbie Member
why we will not create a variable as a datamember in structure?
for eg:
struct some
{
int a;
float g;
struct some n;
};
0
itstimetojazz's Avatar, Join Date: Sep 2009
Light Poster
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.