The compiler may add padding for alignment requirements. Note that this applies not only to padding between the fields of a struct, but also may apply to the end of the struct (so that arrays of the structure type will have each element properly aligned).

For example:

struct foo_t {
    int x;
    char c;
};

Even though the c field doesn't need padding, the struct will generally have a sizeof(struct foo_t) == 8 (on a 32-bit system - rather a system with a 32-bit int type) because there will need to be 3 bytes of padding after the c field.

Note that the padding might not be required by the system (like x86 or Cortex M3) but compilers might still add it for performance reasons.

Answer from Michael Burr on Stack Overflow
🌐
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
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...
Discussions

Calculating struct sizes
figure out what the alignment of various types is on your target architecture. mostly, this should just be alignof(t) = sizeof(t) until you get bigger than a machine word, but you should check for each of your primitive types. a struct's alignment is the highest alignment among its elements. loop through the elements, counting offset starting from zero. before you count an element, increment offset until it's aligned to at least alignof(element). then increment offset by sizeof(element). once you've processed all the elements, increment offset until it's aligned for your whole struct type. a struct's size is always divisible by its alignment, so they can be packed into arrays. offset is now the size of your struct. More on reddit.com
🌐 r/ProgrammingLanguages
9
5
November 21, 2021
sizeof() with struct pointer
Hello folks, i´m just curious is it possible to give a function a struct pointer and it can determine its length ? typedef struct { byte r; byte g; byte b; } RGB; void write_struct(uint8_t i2c_address, uint8_t* … More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
January 17, 2015
sizeof single struct member in C - Stack Overflow
I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic. typedef struct _parent { float calc ; char text[255] ; int used ; } parent_t ... More on stackoverflow.com
🌐 stackoverflow.com
structure member offsetof() and sizeof()
Loading · ×Sorry to interrupt · Refresh More on forum.microchip.com
🌐 forum.microchip.com
🌐
IncludeHelp
includehelp.com › c › size-of-struct-in-c-padding-alignment-in-struct.aspx
Size of struct in C | padding, alignment in struct
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 kind of data type they are pointing too) So the size of the struct should be: (4+8+1+8)=21 Bytes · Let's see what compiler is giving ...
🌐
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 - Prerequisite : sizeof operator in C The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by the compiler to avoid alignment issues.
🌐
Quora
quora.com › How-do-you-find-the-size-of-a-struct-in-C-programming
How to find the size of a struct in C programming - Quora
If you have a struct with a variable length array at the end, like ... sizeof() returns the size in units of size char which is usually one byte, and has return type size_t.
🌐
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
November 1, 2025 - 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.
🌐
Reddit
reddit.com › r/programminglanguages › calculating struct sizes
r/ProgrammingLanguages on Reddit: Calculating struct sizes
November 21, 2021 -

Hi!

I'm creating language that transpiles to C. You can introspect types my language, if the type is a struct, you can access the size of the struct. But this value is NOT calculated at compile time for my compiler ( it simply emits the sizeof operator in C).

This feels a little hacky and ideally i want the information to be available at runtime aswell. This in turn also means, i need to know how much padding the structs will have and the offsets of each member.

But how can i calculate this at compile time? Is there a specific algorithm or method a C compiler ( i use GCC to compile the C code) uses.

tl;dr how can i calculate struct sizes and padding at compile time. (With it being consistent with what GCC will decide, since i transpile to C)

Top answer
1 of 5
13
figure out what the alignment of various types is on your target architecture. mostly, this should just be alignof(t) = sizeof(t) until you get bigger than a machine word, but you should check for each of your primitive types. a struct's alignment is the highest alignment among its elements. loop through the elements, counting offset starting from zero. before you count an element, increment offset until it's aligned to at least alignof(element). then increment offset by sizeof(element). once you've processed all the elements, increment offset until it's aligned for your whole struct type. a struct's size is always divisible by its alignment, so they can be packed into arrays. offset is now the size of your struct.
2 of 5
6
If the structs are only defined in your language, then you don't need to follow C struct rules. Just pack them as you like, or using your own simpler rules that you can understand. In that case, if you are transpiling to C, use: #pragma pack(1) to indicate no alignment or padding to be done by C. Because it is not needed, or it's taken care of by your compiler, which might insert padding fields in the generated C. However if an exact match is required outside your language, eg. you are duplicating an external struct, then it becomes a lot more fiddly. I think the outline has already been given. Which is good as my explanation would be too complex. But here is the code I use in one compiler (part of an interpreter that needs to determine the field offset and overall size of a C struct used in its FFI; not C code): https://github.com/sal55/langs/blob/master/structalign.m Maybe you can glean something from that. This needs to deal also with nested struct/union blocks where the alignment/offset rules change. But that is not necessary if you don't have the same feature.
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
sizeof() with struct pointer - Programming - Arduino Forum
January 17, 2015 - Hello folks, i´m just curious is it possible to give a function a struct pointer and it can determine its length ? typedef struct { byte r; byte g; byte b; } RGB; void write_struct(uint8_t i2c_address, uint8_t* struct_ptr, uint8_t length){ Serial.println(sizeof(&struct_ptr)); } Since struct is not a valid data type i used uint8_t* and length, any better way ?
🌐
OpenGenus
iq.opengenus.org › size-of-struct-in-c
Size of struct in C/ C++
September 16, 2021 - So the size of the struct should be: (4+8+1+8)=21 Bytes · Let's see what compiler is giving using the sizeof() operator.
🌐
FastBit
fastbitlab.com › home › microcontroller embedded c programming lecture 146| sizeof of a structure
sizeof of a structure - Microcontroller Embedded C programming
April 12, 2024 - The size of a structure in C is the sum of the sizes of its members. The size of a structure type can be determined using the sizeof operator in C.
🌐
Aticleworld
aticleworld.com › home › how to find the size of structure in c without using sizeof?
How to find the size of structure in C without using sizeof? - Aticleworld
January 19, 2020 - In C language, sizeof() operator is used to calculate the size of structure, variables, pointers or data types, data types could be pre-defined or user-defined.
🌐
Sub-Etha Software
subethasoftware.com › 2022 › 01 › 18 › c-structures-and-padding-and-sizeof
C structures and padding and sizeof | Sub-Etha Software
March 20, 2024 - The above structure represents three 8-bit byte values and two 16-bit word values for a total of 7 bytes. However, if you were to run this code in GCC for Windows, and print the sizeof() that structure, you would see it returns 10:
🌐
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++?
The size of a struct type element taken by sizeof() is not always equal to the size of each individual member. Sometimes the compilers add some padding to avoid alignment issues. So the size may change. The padding is added when a structure member is followed by a member with a larger size ...
🌐
Linux Hint
linuxhint.com › calculate-value-of-sizeof-struct-c
How do I Calculate the Value of sizeof(struct) in C? – Linux Hint
In C, all you have to do is pair the structure’s name with the sizeof operator to determine the size of the struct.
🌐
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).
🌐
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
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.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 179748-size-struct.html
Size of a struct
November 10, 2020 - #include <stdio.h> struct clientData { unsigned int acctNum; // account number char lastName[15]; // account last name char firstName[10]; // account first name double balance; // account balance }; int main () { printf("%d", sizeof(struct clientData)); } structs are padded for alignment purposes.
🌐
Medium
medium.com › @yash.mundra_80629 › how-structure-size-is-calculated-internally-f60d4635afc
How structure size is calculated internally | by Yash Mundra | Medium
September 11, 2021 - So it make a row which has 4 columns and fill char c in it now it comes to int a now in the row there are 3 empty blocks but int requires 4 blocks so then it creates another row with 4 empty block, Now it comes to char b and again it make a block of size 4. Now at the end we have 3 rows of 4 column so total blocks are 12 hence the size of the above struct is 12.