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

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
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 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
[Need explanation] casting null pointer to get sizeof() struct members
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; } More on reddit.com
🌐 r/C_Programming
15
16
March 19, 2025
🌐
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 of struct #include <stdio.h> int main() { struct A { // sizeof(int) = 4 int x; // Padding of 4 bytes // sizeof(double) = 8 double z; // sizeof(short int) = 2 short int y; // Padding of 6 bytes }; printf("Size of struct: %ld", sizeof(struct A)); return 0; } Output:
🌐
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 ?
🌐
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 ...
🌐
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.
Find elsewhere
🌐
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.
🌐
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++?
July 9, 2012 - In this case the double z is 8-byte long, which is larger than x (4-byte). So another 4-byte padding is added. Also short type data y has 2-byte space in memory so extra 6-bytes are added as padding. #include <stdio.h> struct myStruct { int x; //Integer takes 4 bytes, and padding 4 bytes double z; //Size of double is 8-byte, no padding short int y; //Size of short is 2-byte, padding 6-bytes }; main() { printf("Size of struct: %d", sizeof(struct myStruct)); }
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 43180-sizeof-struct-member-problem.html
sizeof(struct.member) problem
August 8, 2003 - Originally posted by FillYourBrain that's pretty slick actually. I never had a need to do that though. I usually will just sizeof(The Type) instead. Don't structs use a 4 byte grid to align variables ? That might cause some trouble if you try to calculate the size instead of getting it with ...
🌐
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.
🌐
OpenGenus
iq.opengenus.org › size-of-struct-in-c
Size of struct in C/ C++
August 21, 2019 - 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.
🌐
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:
🌐
Wikipedia
en.wikipedia.org › wiki › Sizeof
sizeof - Wikipedia
January 11, 2026 - Here, sizeof buffer is equivalent to 10 * sizeof buffer[0], which evaluates to 10, because the size of the type char is defined as 1. C99 adds support for flexible array members to structures. This form of array declaration is allowed as the last element in structures only, and differs from normal arrays in that no length is specified to the compiler.