Quote:
Originally Posted by troop
Code:
int main () {
sv.exp2 = new EXP2;
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;
}
|
I think he is working in C. However, the problem is the same.
The struct EXP2 does not exist within exp1, only a pointer to a EXP2 struct. In your code you have not created one, thus when you try to dereference the exp2 pointer, you're dereferencing a NULL pointer and it make ka-boom.
Like the C++ code given above, you need to create an EXP2 struct, then link the exp2 pointer in the EXP1 struct to the actual EXP2 struct (sorry, a bit of a mouthful)
You want:
Code:
EXP1 sv;
EXP2 sv_exp2;
int main () {
sv.exp2 = &sv_exp2;
sv.exp2->a = 5 ;
printf("%d",sv.exp2->a) ;
}