Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Typedef Struct - Problems

Typedef Struct - Problems
Thread Tools
Syphor
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Sep 17, 2002, 02:28 AM
 
How will I be able to make this work??

Code:
typedef struct tagA { B bb; } A; typedef struct { A aa; } B;
     
davechen
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Sep 17, 2002, 07:03 PM
 
You can't do it, since the recursive definitions would go on forever.

I.e, if you have a variable foo of type A, you could have

foo.bb.aa.bb.aa.bb...


On the other hand you can do

Code:
typedef struct tagA { struct B *bb; } A; typedef struct { A *aa; } B;
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Sep 18, 2002, 12:17 AM
 
Technically, that wouldn't work either, as the second struct is not defined for the first one to know of its existence.

If you are dealing with classes, it's relatively easy. In the header files, you define one class but don't #import the other. Rather, you claim that it exists by declaring it with @class at the top of the file. You #import the header files from the implementation files. This way, you can have really complicated structures set up using pointers, but the compiler doesn't have to care.

You can do the same thing in to C++ classes using "class" instead of "@class". This also works in Objective-C++.
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
davechen
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Sep 18, 2002, 11:08 AM
 
Originally posted by Detrius:
Technically, that wouldn't work either, as the second struct is not defined for the first one to know of its existence.
Sure it does. Because you're just declaring a pointer to the struct, the compiler doesn't need to know about its size. It's the same thing as in C++ when you use a forward reference to a class.

If you couldn't do this in C you'd never be able to different structures point to each other. I actually compiled the my bit of code to check the syntax.
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Sep 18, 2002, 11:28 AM
 
Originally posted by davechen:


Sure it does. Because you're just declaring a pointer to the struct, the compiler doesn't need to know about its size. It's the same thing as in C++ when you use a forward reference to a class.

If you couldn't do this in C you'd never be able to different structures point to each other. I actually compiled the my bit of code to check the syntax.
I've done very little with C structs. In C++ it wouldn't work without declaring the types. If you didn't declare them, then when the compiler got to the first class, it wouldn't know what the second one is and so it would spit out an error.
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:12 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,