Problems with arrays of unspecified lengths
I'm writing an IrDA stack in c and implementing the Information Access Service component and i need a lookup table for class/key/value pairs. To keep it in an orderly format, I'm trying to put it all into one initialiser. the following code works just fine and compiles the data to compact linked tables in ROM.
#define IAS_PTYPE_STRING 0x00
#define IAS_PTYPE_BYTE 0x01
typedef struct {
UBYTE* name;
UBYTE type;
开发者_StackOverflow中文版UBYTE* value;
} IAS_Attrib_t ;
typedef IAS_Attrib_t* IAS_Attrib_List_t[];
typedef struct {
UBYTE* name;
IAS_Attrib_List_t* attributes;
} IAS_Class_t;
static const IAS_Class_t IAS_Database[] = {
{"IrDA:IrCOMM",
&(IAS_Attrib_List_t){
&(IAS_Attrib_t){"Parameters", IAS_PTYPE_STRING, "IrDA:TinyTP:LsapSel"},
NULL,
},
},
};
However I'm having trouble getting the data back out. according to the types used, i should be able to do something like this:
UBYTE class = 1;
UBYTE attr = 1;
UBYTE* name = (*(IAS_Database[class].attributes))[attr]->name;
this is because
IAS_Database[class].attributes
is typeIAS_Attrib_List_t*
*(IAS_Database[class].attributes)
is typeIAS_Attrib_List_t
i.e.IAS_Attrib_t*[]
(*(IAS_Database[class].attributes))[attr]
should be typeIAS_Attrib_t*
(*(IAS_Database[class].attributes))[attr]->name
should be typeUBYTE*
however when i try to query the table, i get invalid use of array with unspecified bounds
back from mspgcc. Even a hack like (IAS_Attrib_t*)((IAS_Database[class].attributes)+(sizeof(IAS_Attrib_t)*attr))
fails until i cast the db to void like (IAS_Attrib_t*)((void*)(IAS_Database[class].attributes)+(sizeof(IAS_Attrib_t)*attr))
however this just feels so dirty. I'd really like to figure out the correct syntax to do it the right way.
Never mind.
In my utter frustration it seems i tried every variant bar the one I put up here. (*(IAS_Database[class].attributes))[attr]
does indeed work properly and even works when expressed as (*IAS_Database[class].attributes)[attr]
i believe the compiler was incorrectly assuming that the first pointer (being to a UBYTE**) was actually a list and was trying to apply the index to a pointer type (where the type is incomplete).
typedef IAS_Attrib_t* IAS_Attrib_List_t[];
why are you using these brackets ? use a number inside or remove them
精彩评论