Although defining the buffer size with a #define is one idiomatic way to do it, another would be to use a macro like this:

#define member_size(type, member) (sizeof( ((type *)0)->member ))

and use it like this:

typedef struct
{
    float calc;
    char text[255];
    int used;
} Parent;

typedef struct
{
    char flag;
    char text[member_size(Parent, text)];
    int used;
} Child;

I'm actually a bit surprised that sizeof(((type *)0)->member) is even allowed as a constant expression. Cool stuff.

Answer from Joey Adams on Stack Overflow
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 43180-sizeof-struct-member-problem.html
sizeof(struct.member) problem
December 21, 2021 - He asked for the size of a structmember without having to create an instance of the struct, if I got it right I guess I would try something like sizeof(TMyStruct::iMyIntMember); edit: whoop, doesn't work unless you want a to be static... Last edited by darksaidin; 08-08-2003 at 02:28 AM. ... When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] ... >How do I find the length of a member of mystruct without first declaring it etc?
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ [need explanation] casting null pointer to get sizeof() struct members
r/C_Programming on Reddit: [Need explanation] casting null pointer to get sizeof() struct members
March 19, 2025 -

In this Stackoverflow post[1] is stumbled upon a 'trick' to get the size of struct members like so: sizeof(((struct*)0)->member) which I struggle to comprehend what's happening here.

what I understand:
- sizeof calculates the size, as normal
- ->member dereferences as usual

what I don't understand:
- (struct*) 0 is a typecast (?) of a nullptr (?) to address 0 (?)

Can someone dissect this syntax and explain in detail what happens under the hood?

[1] https://stackoverflow.com/a/3553321/18918472

Top answer
1 of 5
31
It helps to go back to first principals and build it up. Given a struct: struct foo { int a; }; The problem is sizeof() ONLY works on FULL types or instances not types of member fields. We CAN'T do this to get at a struct's member field ... printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR ... so we need to use a temporary. struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK We could use a pointer instead, and deference that pointer: struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK However, the address of the temporary doesn't matter! We could have a pointer where the instance of foo is at address 0! struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK We can remove the temporary entirely by inlining it -- pretending we have the struct at address 0. printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK We can turn that into a macro to help readability: #define MEMBER_SIZE( type, member ) (sizeof( ((struct type *)0)->member )) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK If you already know the struct type you can avoid passing the type. #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK Demo below: #include struct foo { int a; }; int main() { printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK #define MEMBER_SIZE( type, member ) sizeof( ((struct type*)0)->member ) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK return 0; }
2 of 5
10
sizeof doesn't evaluate the operand unless it's a variable length array. So, the cast and -> are fine as they're not evaluated. After all, the sizeof operator only needs to know the type of the operand to figure out the size. So, there's no point at all in evaluating the value of the operand expression (unless it's a variable length array).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ why-isn-t-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member-in-c-cplusplus
Why isn't sizeof for a struct equal to the sum of sizeof of each member in C/C++?
#include <stdio.h> struct myStruct { double z; //Size of double is 8-byte, no padding int x; //Integer takes 4 bytes, and padding 4 bytes short int y; //Size of short is 2-byte, padding 6-bytes }; main() { printf("Size of struct: %d", sizeof(struct myStruct)); } ... In the third case it also takes 16-byte of memory space, but the arrangements are different. As the first member is double then it is placed at first, then the short type data is added.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ is-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member
Is sizeof for a struct equal to the sum of sizeof of each member? - GeeksforGeeks
July 11, 2025 - // C program to illustrate // size ... return 0; } Output: Size of struct: 16 In this case, the members of the structure are sorted in decreasing order of their sizes....
๐ŸŒ
Bytes
bytes.com โ€บ home โ€บ forum โ€บ topic
sizeof(struct::member) - Post.Byes
August 18, 2025 - Steinbach:[color=blue] > * Rafal Dabrowa:[color=green] > > Suppose we have the following structure: > > > > struct foo { > > int x, y; > > }; > > > > Is there a possibility to get sizeof(foo::x) when > > no variable of type foo is available ?[/color] > > A common hack is > > foo fooObject(); // Not implemented. > > std::size_t const s = sizeof( fooObject().x ); > > If you like to live dangerously you can alternatively just > cast 0 to a pointer to foo and dereference it, but although > it should be practically safe (sizeof arguments aren't evaluated > at run time) I'm not sure about its formal legality: if it were > all OK then presumably the hack above would be less used.[/color] Forgot to mention: in your particular example case you don't have to worry about constructor availability, so you could just write sizeof( foo().x ) but that won't work in general.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-I-calculate-the-value-of-sizeof-struct-in-C
How to calculate the value of sizeof(struct) in C - Quora
November 1, 2025 - Answer (1 of 8): The exact sequence [code ]sizeof(struct)[/code] is a compile error in C. I assume youโ€™re asking how you determine the size of a particular [code ]struct[/code]. That answer is not provided by the C language definition directly. Rather, itโ€™s a function of the ABI for your particu...
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ size-of-struct-in-c
Size of struct in C/ C++
January 11, 2026 - Size of the struct should be sum of all the data member, which is: Size of int n1+ size of int* n2 +size of char c1+ size of char* c2 ยท Now considering the 64-bit system, Size of int is 4 Bytes Size of character is 1 Byte Size of any pointer type is 8 Bytes (Pointer size doesn't depend on ...
๐ŸŒ
SEI CERT
wiki.sei.cmu.edu โ€บ confluence โ€บ display โ€บ c โ€บ EXP03-C.+Do+not+assume+the+size+of+a+structure+is+the+sum+of+the+sizes+of+its+members
EXP03-C. Do not assume the size of a structure is the sum of the sizes of its members - SEI CERT C Coding Standard - Confluence
January 1, 2015 - It's impossible for a static analysis tool to infer the intentions of a programmer, so ROSE can't tell that the programmer summed up the size of a struct's elements and substituted that for the size of a struct. Besides, such an error would cause a programmer to issue magic numbers (54) to a malloc function, or some function that expected a sizeof() operator.
๐ŸŒ
Delorie
delorie.com โ€บ djgpp โ€บ v2faq โ€บ faq22_11.html
22.11 What should sizeof (struct xyzzy) return?
If you do need this method, be ... the sum of the members' sizes, even if you instructed the compiler to pack structs: GCC still can add some padding after the last member. So always use sizeof struct foo to read and write a structure....
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Flexible_array_member
Flexible array member - Wikipedia
February 7, 2026 - The sizeof operator on such a struct gives the size of the structure as if the flexible array member were empty.
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ sizeof-single-struct-member-in-c
Pointers: Size of a single member in a C struct
November 1, 2021 - Sizeof single struct member in C, @XavierGeoffrey: Here, sizeof infers the type of ((type *)0)->member, and returns the size of that type. ((type *)0) is just a null pointer of the struct type. ptr->member is (at compile time) an expression whose type is that of the member.
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ c-snippets โ€บ determine-size-of-structure-and-why-structure-size-is-different-in-c.php
C โ€“ Find the size of a structure
August 8, 2003 - C โ€“ Find the size of a structure. The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most sytems, sizeof( int ) would yield 4, as would sizeof(number) if number were a variable of type int.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 43965
sizeof struct with one member - C++ Forum
November 11, 2023 - thanks for your answer. I had already tested it when I asked this question. The result was sizeof(S) == sizeof(int) of course. But the standard says this depends on the architecture so my question goes beyond the practical test of this case. I'll try to make my question clearer.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c โ€บ size-of-struct-in-c-padding-alignment-in-struct.aspx
Size of struct in C | padding, alignment in struct
July 18, 2019 - Size of the struct should be sum of all the data member, which is: Size of int a+ size of int* b +size of char c+ size of char* d ยท Now considering the 64-bit system, Size of int is 4 Bytes Size of character is 1 Byte Size of any pointer type is 8 Bytes (Pointer size doesn't depend on what ...