Reputation: 28880
I've got this code, which I don't understand why it doesn't compile:
typedef struct
{
uint32_t serial_number;
uint32_t ieee_address[6];
} FACTORY_CONFIG;
...
// Unlock flash (only the portion we write on)
error = FLASHD_Unlock ( writeaddress, writeaddress + sizeof ( FACTORY_CONFIG.serial_number ), 0, 0 );
When I run it, I get this error:
Error[Pe018]: expected a ")"
When I change the
FACTORY_CONFIG.serial_number
to
FACTORY_CONFIG
, it compiles and everything works. I'm not sure, can I check the size of a type inside a structure ?
Upvotes: 1
Views: 205
Reputation: 1354
You need to create the object first. The struct you are creating is only a type. Do like this:
sizeof (((FACTORY_CONFIG *)0)->serial_number)
Upvotes: 0
Reputation: 78903
What you seem to want, ist to interpret your writeaddress
as if it were pointing to an object of your struct
and then access the address of the ieee_address
member.
The way such things are meant to be done in C is
((FACTORY_CONFIG*)writeaddress)->ieee_address
that's it.
The method you were using is very dangerous because you don't know how your compiler will layout the struct
in memory. If you have to, there is the offsetof
macro to get the exact postion of a field.
BTW, having type alias in all caps is against common coding style and against everybody's habits. Usually all caps names are reserved for macros.
Upvotes: 0
Reputation: 273456
You can't just access members of types in C like that. You can, however, take sizeof
from actual objects. And since sizeof
is a compile-time construct, these objects don't even have to be valid. So the following will work:
sizeof(((FACTORY_CONFIG *)0)->serial_number)
If you use this a lot, or just for readability, you could make a macro out of it.
Upvotes: 3