Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C program to display the size and offset of structure members
In C programming, structures allow us to group different data types under a single name. When working with structures, it's important to understand how memory is allocated for each member and their positions within the structure. The sizeof() operator helps determine the size of structure members, while the offsetof() macro shows the byte offset of each member from the beginning of the structure.
Syntax
struct structure_name {
datatype member1;
datatype member2;
datatype memberN;
};
sizeof(variable_or_type)
offsetof(struct_type, member_name)
Structure Declaration
A structure is declared using the struct keyword followed by a tag name and member variables −
struct book {
int pages;
char author[30];
float price;
};
Example: Displaying Size and Offset of Structure Members
The following program demonstrates how to find the size of individual structure members and their memory offsets −
#include <stdio.h>
#include <stddef.h>
struct tutorial {
int a;
int b;
char c[4];
float d;
double e;
};
int main() {
struct tutorial t1;
printf("Size of structure members:<br>");
printf("Size of 'a' (int): %lu bytes<br>", sizeof(t1.a));
printf("Size of 'b' (int): %lu bytes<br>", sizeof(t1.b));
printf("Size of 'c' (char[4]): %lu bytes<br>", sizeof(t1.c));
printf("Size of 'd' (float): %lu bytes<br>", sizeof(t1.d));
printf("Size of 'e' (double): %lu bytes<br>", sizeof(t1.e));
printf("\nOffset of structure members:<br>");
printf("Offset of 'a': %lu bytes<br>", offsetof(struct tutorial, a));
printf("Offset of 'b': %lu bytes<br>", offsetof(struct tutorial, b));
printf("Offset of 'c': %lu bytes<br>", offsetof(struct tutorial, c));
printf("Offset of 'd': %lu bytes<br>", offsetof(struct tutorial, d));
printf("Offset of 'e': %lu bytes<br>", offsetof(struct tutorial, e));
printf("\nTotal size of structure: %lu bytes<br>", sizeof(t1));
return 0;
}
Size of structure members: Size of 'a' (int): 4 bytes Size of 'b' (int): 4 bytes Size of 'c' (char[4]): 4 bytes Size of 'd' (float): 4 bytes Size of 'e' (double): 8 bytes Offset of structure members: Offset of 'a': 0 bytes Offset of 'b': 4 bytes Offset of 'c': 8 bytes Offset of 'd': 12 bytes Offset of 'e': 16 bytes Total size of structure: 24 bytes
Memory Layout Explanation
The memory layout shows how structure padding affects the total size. Even though the sum of individual member sizes is 20 bytes (4+4+4+4+8), the structure occupies 24 bytes due to alignment requirements for the double member.
Key Points
- The
sizeof()operator returns the size in bytes of a variable or data type. - The
offsetof()macro requires<stddef.h>header and returns the byte offset of a structure member. - Structure padding may cause the total size to be larger than the sum of individual member sizes.
- Member alignment is based on the largest data type in the structure.
Conclusion
Understanding structure member sizes and offsets is crucial for memory optimization and debugging. The sizeof() and offsetof() functions provide valuable insights into how the compiler organizes structure data in memory.
