views:

914

answers:

2

Hi,

Can someone confirm that nested C structures are NOT allowed in objective C.

And by nested structs, I mean the following;

struct Tex2D
{
    GLfloat u;
    GLfloat v;
};

// quad uv cords
struct TexQuad
{
    Tex2D uv[4];
};

I seem to have all kinds of problems compiling this. It's difficult to find any documentation on this as this is perfectly valid c code.

Cheers Rich

+5  A: 

Like in C you have to use the keyword struct when referencing structs. :-)

// quad uv cords
struct TexQuad
{
    struct Tex2D       uv[4];
};

Works!

Thanks, damn I haven't done C in about 10 years ... I've become so used to c++. Thanks. Both answers are correct however I can only tick on of them. I ticked the one below as it was more verbose.
Rich
+7  A: 

What you have there is not valid C code. Remember, in C, when you declare a struct variable, you have to explicitly refer to it as a struct, like this:

struct StructType myStruct;

The most common way I've seen this solved is to bundle the struct declaration with a typedef, like this:

typedef struct _Tex2D
{
    GLfloat     u;
    GLfloat     v;
} Tex2D;

// quad uv cords
typedef struct _TexQuad
{
    Tex2D       uv[4];
} TexQuad;

That way, you can then create new struct variables without having to use the struct keyword.

htw
Thanks, damn I haven't done C in about 10 years ... I've become so used to c++. Thanks. Both answers are correct however I can only tick on of them. I ticked this one as it was more verbose.
Rich