Discussion Locked

Nested Structures

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How many nested structures we can have in one structure in C or C++ Language

9 replies

by lastorck

i think u can check it in your compiler's documentations. it wouldn't strictly limited. but i think it isn't a good idea to define too much nested structures - maybe better declare them separately and use typed variables of structures as nested elements of other structures (recursivetly ofcourse)
Regards

by basilisk

I did a program once with 6 or 7 levels of nested structs (done with typedefs to keep the names of the parts meaningful) and my gnu compiler didn't complain. Trying to reference any atomic value in that struct was a nightmare.

I suspect that youwill become unable to keep track of the nesting mentally long before your compiler has problems. Which is another argument for moving to a OO approach and using well defined and tested classes, with lots of useful and meaningfully-named member functions.

by cedric king

I agree with both the answers offered. It does not really matters how deep is your structure. It depends on the fact whether you can remember what goes where and it's about good programming practice. You as the programmer can understand how your nested structures goes, any one else who takes over your codes will not. He might not know why you nest one structure in another and stuff like that.

by ruler

I agree with the above responses in that C/C++ doesn't have a hard limit that one will reach when programming most applications. (There may be one, but it'll most likely be exceedingly high.) I've personally had about a dozen or so members of other structure types in a structure without any problems. When you think about what the compiler does when it compiles and assembles your code, it doesn't make sense to enforce a limit on the number of structure members you can have as data members of another structures; everything resolves to either a specific memory addresses or a pointer to memory addresses.

This discussion is closed to new replies.