![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
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;
#endifb.h: #ifndef _B_H_
#define _B_H_
#include"a.h"
typedef struct{
a_struct as;
} b_struct;
#endifc.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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
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] |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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.
![]() |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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).
|
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
Quote:
![]() 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] |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
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. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|