How can I get the size of a member in a struct in C?
struct A
{
char arr[64];
};
i need something like that:
sizeof(A::arr)
thanks
sizeof(((struct A*)0)->arr);
Briefly, cast a null pointer to a type of struct A*
, but since the operand of sizeof
is not evaluated, this is legal and allows you to get size of struct members without creating an instance of the struct.
Basically, we are pretending that an instance of it exists at address 0 and can be used for offset and sizeof
determination.
To further elaborate, read this article:
http://www.embedded.com/design/prototyping-and-development/4024941/Learn-a-new-trick-with-the-offsetof--macro
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With