开发者

sizeof a struct member [duplicate]

开发者 https://www.devze.com 2023-01-18 23:54 出处:网络
This question already has answers here: sizeof single struct member in C (9 answers) 开发者_StackOverflow中文版
This question already has answers here: sizeof single struct member in C (9 answers) 开发者_StackOverflow中文版 Closed 9 years ago.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消