Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 6th, 2005, 9:22 AM   #1
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
c struct : problems whit structs containing each other

Hi

I'm having problems compiling structs. I tried to find a solution from my K&R C book and the web, but couldn't.

Basically I'm trying to have two structs that both contain member whose type is the other struct. I've tried a number of ways to get it to work, but have had no luck.

Here's my latest attempt ( simplified from my code) :

a.h:

#ifndef _A_H_ 
#define _A_H_ 
#include"b.h"

typedef struct{
    b_struct bs; 
} a_struct;
#endif

b.h:

#ifndef _B_H_
#define _B_H_ 
#include"a.h"

typedef struct{
    a_struct as;
} b_struct;
#endif

c.c:
#include"a.h"


When I try to compile, I get:

$ gcc c.c
In file included from a.h:4,
from c.c:1:
b.h:7: error: parse error before "a_struct"
b.h:7: warning: no semicolon at end of struct or union
b.h:8: warning: data definition has no type or storage class
In file included from c.c:1:
a.h:7: error: parse error before "b_struct"
a.h:7: warning: no semicolon at end of struct or union
a.h:8: warning: data definition has no type or storage class

If someone could tell me how to modify my code so that it would compile, or propose a different way of creating two structs that both have a member whose type is the other, it would be greatly appreciated. It would be even better if someone could explain to me, why my code doesn't compile but the correct code having those two structs does.

Thanks for reading and for any advice/pointers to advice in advance.
mevuorin is offline   Reply With Quote
Old Jul 6th, 2005, 1:01 PM   #2
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
struct first;

struct second {
   int x;
   struct first * f;
};

struct first {
   int y;
   struct second * g;
};

Works just fine for me.....
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Jul 6th, 2005, 5:55 PM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
L7sqr - the reason it works is because you created a pointer to a struct, pointers have size which is always known by the compiler. Drop the * and see if you can make it work with a forward reference.
jim mcnamara is offline   Reply With Quote
Old Jul 6th, 2005, 6:12 PM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
The problem is because the include files can only be included once. That's your syntax errors. The other problem is the fact that you can't do that in C. A b_struct contains an a_struct which contains a b_struct which contains an a_struct, forever.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 6th, 2005, 6:51 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Yes you can. L7Sqr demonstrated it quite well. If you want to apply it to mevuorin's stuff, here's how it works:

/* globals.h */
struct _a_struct;
struct _b_struct;

/* a.h */
#ifndef _A_H_ 
#define _A_H_ 

#include "b.h"

typedef struct _a_struct {
    b_struct bs; 
} a_struct;

#endif

/* b.h */
#ifndef _B_H_
#define _B_H_ 

#include "a.h"

typedef struct _b_struct {
    a_struct as;
} b_struct;

#endif
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 6th, 2005, 8:22 PM   #6
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
He did it with pointers you commie bastard!
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 6th, 2005, 9:18 PM   #7
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
ooble your code will not work, forward defines can only be used to declare references (c++) or pointers. In your code the compiler work know the size of the forward declared structures. This problem can only be solved by using pointers in one, or both, of the structues (as shown in L7Sqr's code).
Animatronic is offline   Reply With Quote
Old Jul 6th, 2005, 9:53 PM   #8
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
nyah, nyah.
Oobs was wrong!
(note to self: increase quality of future posts.)
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 6th, 2005, 10:16 PM   #9
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
Quote:
Originally Posted by jim mcnamara
L7sqr - the reason it works is because you created a pointer to a struct, pointers have size which is always known by the compiler. Drop the * and see if you can make it work with a forward reference.
c'mon, don't ruin ALL the fun

I was merely showing how the problem could be solved - no constraint was applied

Although, doing 'exactly' what was asked is not possible - as is well pointed out by previous posts.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Jul 7th, 2005, 3:58 AM   #10
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
Thanks to everyone for help. Now I understand what the problem was and managed to make my structs work.

Here's the solution I ended up with:
//A.h
#ifndef _A_H_
#define _A_H_

#include"b.h"

typedef struct _a_struct{
	struct _b_struct *bs;
} a_struct;

#endif /*_A_H_*/

//B.h
#ifndef _B_H_
#define _B_H_

#include"a.h"

typedef struct _b_struct{
	struct _a_struct *as;
} b_struct;

#endif /*_B_H_*/

//C.c
#include"a.h"

Compiles and works. Finally.

Big thanks to everyone again. That struct problem had made my head hurt for few days. You cured it. I think I'll post here more.
mevuorin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC