LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   structure within a structure in C (http://www.linuxquestions.org/questions/programming-9/structure-within-a-structure-in-c-535051/)

knobby67 03-06-2007 09:07 AM

structure within a structure in C
 
Hi,
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
struct A_STRUCTURE
{
float a;
float b;
float c;
};


Then

struct B_STRUCTURE
{
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 )

Thanks

tuxdev 03-06-2007 09:34 AM

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

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

My personal preference to use "struct A_STRUCTURE".

kaz2100 03-06-2007 09:34 AM

Hi,

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

struct B_STR{
struct A_str loc;
/*^^^^*/
float b;
}

or typedef.

Happy Penguins!

knobby67 03-06-2007 10: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 10:04 PM.