Advertisement

Linked Lists

Started by March 14, 2003 02:26 PM
4 comments, last by adam17 21 years, 11 months ago
is it possible to have a linked list of structs within structs? something like this struct Struct1 { int num1; }; struct Struct2 { Struct1 str1; Struct2 *pnext; }; Struct2 teststruct; now how, if possible, would i access num1 using teststruct?
teststruct.str1.num1

or, since you spoke of linked lists

if(pnext != NULL){
teststruct.pnext->str1.num1
}

I am not sure if you can make a declaration of a struct within itself, so you could try

struct Struct2
{
Struct1 str1;
void *pnext;
}

then access with a cast
((Struct2 *)pnext)->str1.num1

[edited by - variant on March 14, 2003 3:47:33 PM]
Advertisement
quote:
Original post by variant
I am not sure if you can make a declaration of a struct within itself, so you could try


Don''t worry, you can do that

The pointer was ok as it was...What''s not allowed is this:


  struct Struct2 {Struct1 obj1;Struct2 infiniteMemoryUseIsNotPossibleSoCompileError;}  
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
You can''t declare a structure with the tag unless you prefix it with the struct keyword.

like this

struct mystruct {     int data;     struct mystruct* next;}struct mystruct* head; 


Thats why we have typedef...

-Will
quote:
Original post by AcidInjury
You can''t declare a structure with the tag unless you prefix it with the struct keyword.
You can in c++.

This topic is closed to new replies.

Advertisement