-   Programming (
-   -   structure within a structure in C (

knobby67 03-06-2007 08:07 AM

structure within a structure in C
I've been asked to transfer a bit of code from C++ to C. But am having a small bit of trouble with a .h file. Basically it calls a structure from a structure.

so I have
float a;
float b;
float c;


A_STRUCTURE location; <- problem line
float d;

When I build I get the follwing error

test.h:37: error: syntax error before A_STRUCTURE

I'm sure I could move A_STRUCTURE to another .h file, but other that doing that can I decalre a structure calling a structure in a header file in C? (If that makes sense :s )


tuxdev 03-06-2007 08:34 AM

In C, you either have to use "struct A_STRUCTURE" whenever you reference that type, or use the typedef idiom:

typedef struct {
  float a;
  float b;
  float c;

My personal preference to use "struct A_STRUCTURE".

kaz2100 03-06-2007 08:34 AM


You either have to add struct before A_ in definiton of B_

struct B_STR{
struct A_str loc;
float b;

or typedef.

Happy Penguins!

knobby67 03-06-2007 09:00 AM

Thanks :D all worked went for struct. All my years programming C and I never knew that. It's nice to learn something so usefull but so easy.

All times are GMT -5. The time now is 01:52 AM.