Click here to Skip to main content
16,011,538 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Chesnokov Yuriy2-Jul-08 22:04
professionalChesnokov Yuriy2-Jul-08 22:04 
AnswerRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Stephen Hewitt2-Jul-08 22:09
Stephen Hewitt2-Jul-08 22:09 
GeneralRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
CPallini2-Jul-08 22:19
mveCPallini2-Jul-08 22:19 
QuestionRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Chesnokov Yuriy2-Jul-08 22:34
professionalChesnokov Yuriy2-Jul-08 22:34 
AnswerRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Stephen Hewitt2-Jul-08 22:37
Stephen Hewitt2-Jul-08 22:37 
AnswerRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Chesnokov Yuriy2-Jul-08 23:05
professionalChesnokov Yuriy2-Jul-08 23:05 
GeneralRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Stephen Hewitt6-Jul-08 14:56
Stephen Hewitt6-Jul-08 14:56 
AnswerRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Iain Clarke, Warrior Programmer2-Jul-08 22:24
Iain Clarke, Warrior Programmer2-Jul-08 22:24 
You're having trouble with them being declared as pointers, and want them as real contained structs.

To borrow a phrase, "Imagine if you could do what you're asking".

AA would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB which would contain an AA which would contain a BB...

I hope I'm getting the point across - I'm bored of typing. But your structure would end up infinitely large.

You can contain a *pointer* to your structures evil twin - a pointer only takes up a few bytes. But you need to give the pointer something to point to.

So:
struct BB;

struct AA
{
    struct BB *bb;
    int a;
};
struct BB
{
    struct AA *aa;
    int b;
};

main ()
{
    AA a;
    BB b;

    a.bb->b = 1;   // Will compile, but will crash horribly, because a.bb doesn't have any useful value in it.
}


In the above example, C isn't psychic, so hasn't pointed A.bb at any BB structure. How can it? That's your job.

Try the below code.

AA a;
BB b;

a.bb = &b;
b.aa = &a;

a.bb->b = 1;
b.aa->a = 3;


Now your two structure can talk to each other to their heart's content.

I'd go back to your C book, and look up linked lists - it'll give you a light bulb moment about pointers.

Iain.

Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

GeneralRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
CPallini2-Jul-08 22:28
mveCPallini2-Jul-08 22:28 
QuestionRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Chesnokov Yuriy2-Jul-08 22:38
professionalChesnokov Yuriy2-Jul-08 22:38 
AnswerRe: How to struct AA{ struct BB; }; struct BB { struct AA; }; Pin
Iain Clarke, Warrior Programmer2-Jul-08 22:49
Iain Clarke, Warrior Programmer2-Jul-08 22:49 
GeneralOT Pin
CPallini2-Jul-08 23:06
mveCPallini2-Jul-08 23:06 
QuestionHelp with CTabCtrl Pin
rbmarasigan20012-Jul-08 20:42
rbmarasigan20012-Jul-08 20:42 
AnswerRe: Help with CTabCtrl Pin
Iain Clarke, Warrior Programmer2-Jul-08 22:39
Iain Clarke, Warrior Programmer2-Jul-08 22:39 
QuestionGPL Pin
Dennis L2-Jul-08 20:35
Dennis L2-Jul-08 20:35 
AnswerRe: GPL Pin
Hamid_RT2-Jul-08 21:31
Hamid_RT2-Jul-08 21:31 
GeneralRe: GPL Pin
Dennis L2-Jul-08 21:54
Dennis L2-Jul-08 21:54 
AnswerRe: GPL - and bullying hippy teachers. Pin
Iain Clarke, Warrior Programmer2-Jul-08 23:20
Iain Clarke, Warrior Programmer2-Jul-08 23:20 
Questionheader-files Pin
testcrap2-Jul-08 20:03
testcrap2-Jul-08 20:03 
AnswerRe: header-files Pin
Stephen Hewitt2-Jul-08 20:13
Stephen Hewitt2-Jul-08 20:13 
GeneralRe: header-files Pin
Stephen Hewitt2-Jul-08 21:04
Stephen Hewitt2-Jul-08 21:04 
GeneralRe: header-files Pin
Iain Clarke, Warrior Programmer2-Jul-08 23:21
Iain Clarke, Warrior Programmer2-Jul-08 23:21 
AnswerRe: header-files Pin
Stephen Hewitt2-Jul-08 20:22
Stephen Hewitt2-Jul-08 20:22 
GeneralRe: header-files Pin
testcrap2-Jul-08 20:39
testcrap2-Jul-08 20:39 
QuestionEasy Question Pin
C# Beginner Nick2-Jul-08 18:40
C# Beginner Nick2-Jul-08 18:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.