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. Using the sizeof() operator we can calculate the size of the structure straightforward to pass it as a parameter.
But here I am interested in calculating the size of structure in C without using the sizeof() operator. Calculating the size of structure in c without using the sizeof() operator seems to be difficult but with the help of pointers, we can do it easily. It is also an important interview question that is generally asked by the interviewer to check the understanding of pointer.
In this article, I will describe some methods to calculate the size of the structure in c without using the sizeof the operator. But it is my recommendation to use the sizeof() operator to calculate the size of the structure in a program whenever you need to calculate the size of the structure.
Here we need to use some techniques to calculate the size of the structure in C. I am describing this technique, using a few examples.
Note: Structure padding also affects the size of the structure in C.
Example 1
- First, create the structure.
- After creating the structure, create an array of structures, Here RamInfo[2].
- Create a pointer to structure and assign the address of the array.
#include <stdio.h> #include <stdlib.h> typedef struct { char Name[12]; int Age; float Weight; int RollNumber; } sStudentInfo; int main(int argc, char *argv[]) { //create an array of structure; sStudentInfo RamInfo[2] = {0}; //Create pointer to the structure sStudentInfo *psInfo = NULL; int iSizeofStructure = 0; //Assign the address of array to the pointer psInfo = RamInfo; // Subtract the pointer iSizeofStructure = (char*)(psInfo + 1) - (char*)(psInfo); printf("Size of structure = %d\n\n",iSizeofStructure); return 0; }
Example 2
When we increment the pointer then pointer increases a block of memory (block of memory depends on pointer data type). So here we will use this technique to calculate the size of a structure.
See this link: pointer arithmetic
- First, create the structure.
- Create a pointer to structure and assign the NULL pointer.
- Increment the pointer to 1.
#include <stdio.h> typedef struct { char Name[12]; int Age; float Weight; int RollNumber; } sStudentInfo; int main(int argc, char *argv[]) { //Create pointer to the structure sStudentInfo *psInfo = NULL; //Increment the pointer psInfo++; printf("Size of structure = %u\n\n",psInfo); return 0; }
If you want to learn more about the c language, here 10 Free days C video course for you.
Example 3
We can also calculate the size of the structure using the pointer subtraction. In the previous article “all about the pointer“, we have read that using the pointer subtraction we can calculate the number of bytes between the two pointers.
- First, create the structure.
- create an array of structures, Here aiData[2].
- Create pointers to the structure and assign the address of the first and second element of the array.
- Subtract the pointers to get the size of the structure in c.
#include <stdio.h> #include <stdlib.h> typedef struct { char Name[12]; int Age; float Weight; int RollNumber; } sStudentInfo; int main(int argc, char *argv[]) { //create an array of structure; sStudentInfo aiData[2] = {0}; //Create two pointer to the integer sStudentInfo *piData1 = NULL; sStudentInfo *piData2 = NULL; int iSizeofStructure = 0; //Assign the address of array first element to the pointer piData1 = &aiData[0]; //Assign the address of array third element to the pointer piData2 = &aiData[1]; // Subtract the pointer iSizeofStructure = (char*)piData2 - (char *)piData1; printf("Size of structure = %d\n\n",iSizeofStructure); }
Example 4
- First, create the structure.
- After creating the structure, create an array of structures, Here sData[2].
- Get the address of the first element using the sData[0] and sData[1].
- Subtract both addresses to get the size of the structure.
#include<stdio.h> struct { int a; int b; } sData[2]; int main() { int start, last; start = &sData[1].a; last = &sData[0].a; printf("\nSize of Structure : %d Bytes",start-last); return 0; }
Output: 8 byte
Is sizeof for a struct equal to the sum of sizeof of each member?
No. The sizeof of a structure is not always equal to the sum of sizeof of each individual member. This is because of the extra padding byte which inserts by the compiler to avoid alignment issues. According to the C standards, alignment of structure totally depends on the implementation.
Let us see an example for better understanding:
#include <stdio.h> typedef struct { // sizeof(char) = 1 char A; // sizeof(int) = 4 int B; // sizeof(char) = 1 char C; } InfoData; int main(int argc, char *argv[]) { //Calculate size of structure printf("\n Size of Structure = %d\n\n",sizeof(InfoData)); return 0; }
Output: 12
In the above declaration, Integer is the largest structure member (4 bytes) so to prevent the performance penalty compiler inserts some extra padding bytes to improve the performance of the CPU. So the size of the InfoData will be 12 bytes due to the padding bytes.
Recommended Posts for you
- structure in C: you should know in depth
- structure padding, you should know.
- What is flexible array member in c?
- What is importance of struct hack in c?
- Best structure padding questions.
- How to access pointer inside a structure in c.
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- Memory Layout in C.
- Union in C, A detailed Guide.
- typedef vs #define in C.
- Macro in C, with example code.
- enum in C, you should know.
- You should know the volatile Qualifier.
- 100 C interview Questions.
- Interview questions on bitwise operators in C
- 10 questions about dynamic memory allocation.
- File handling in C.
- Pointer in C.
- C format specifiers.