In gcc it is possible to create an anonymous struct, like this:
struct test2 {
struct {
int x;
int y;
};
int z;
};
But when I try to do the same thing with a pre-defined version of the anonymous struct, it fails:
struct test1 {
int x;
int y;
};
struct test2 {
struct test1;
int z;
};
The reason I want to do this is that I want to define a struct which contains a set of function pointers corresponding to “member functions” of a C class. If I don’t include the struct anonymously, then I wind up having to do something obnoxious like this:
object->API->function(object,...);
which just plain doesn’t work for some things I need to do (for instance, since “object.API” doesn’t match “object”, it makes it difficult or impossible to do automatic bindings from other languages which support hiding of the object pointer.)
At the moment, I just copy/paste the functions, but this is tedious and error prone. I could use the C pre-processor, but it seems just ugly to create a macro which has a bunch of function prototypes in it.
struct test1is the name of the type of the struct. You are lacking a name for the field (and you cannot have “anonymous” fields other than inline-defined structs or unions).EDIT: Apparently there’s a GCC extension that allows you do that:
-fms-extensions