-
Nested structures
I'm not sure about this one "aspect", if you may. Would it be better to do this:
Code:
struct student {
char name[50];
struct student *next_stud;
struct grade {
int grade;
struct grade *next_grad;
};
};
and have nested structures, or better to do this:
Code:
struct grade {
int grade;
struct grade *next_grad;
};
struct student {
char name[50];
struct student *next_stud;
struct grade *grad;
};
I don't think there is much of a difference, is there? I just didn't nest the structures the second time. Thanks.
--Garfield
-
The first one should be
Code:
struct student {
char name[50];
struct student *next_stud;
struct grade {
int grade;
struct grade *next_grad;
} *grad;
};
I think they're the same.
-
Oh, thanks. I forgot that I did have to "name", if you may, the grade struct. Thanks!
--Garfield
-
tha difference is "Scope" if you need the grade struct globaly then you will not be able to use it if it is nested while if it is not nested it can be used anywhere.
so if you need a struct for only a single use in another struct then you could but don't have to, nest it in that struct.
BTW: i may be quoting C++ scope rules but i don't think so.
-
> tha difference is "Scope" if you need the grade struct globaly then you will not be able to use it if it is nested while if it is not nested it can be used anywhere.
That's exactly what I thought, until I tried it, but it didn't seem to be the case.
I tried this with
gcc version 2.8.1
Borland C++ 5.3 for Win32
and both saw the nested structure at the global scope.
However, compiling it as C++ does draw a scope diagnostic.
-
>
I tried this with
gcc version 2.8.1
Borland C++ 5.3 for Win32
and both saw the nested structure at the global scope
<
guess i am spoutin C++ scope rules, i tried and get the same.
-
But is that scope of the nested structure actually possible to be global? This example:
Code:
struct student {
char name[50];
struct student *next_stud;
struct grade {
int grade;
struct grade *next_grad;
} *grad;
};
You guys stated that struct grade (grad) actually has the global scope. So, then does char array 'name' have a global scope? Shouldn't it follow the same scope rules?
--Garfield
-
>
You guys stated that struct grade (grad) actually has the global scope. So, then does char array 'name' have a global scope? Shouldn't it follow the same scope rules?
<
to the first question no, 'name' does not have global scope.
to the second, it would be more likely that the struct should follow the same scope rules as the other data in order to maintain data hiding.
but since the struct 'grade' is not technically a data member of struct 'student' and does not require data hiding,it is seen as global.
at least thats my guess.
-