How to find the Size of structure without sizeof() Operator?

In this tutorial, we will see how to find the size of a structure without using the sizeof() operator. This is a commonly asked question during coding interviews. 

As you know, a structure consists of collections of multiple data members of different types. In the C/C++ programming language,  structures are defined using the keyword struct and it can store one or more data elements in it. A simple structure example is shown in the code below:

typedef struct
{  
    int a;   
    char b;   
    char c;  
}sample; 

As it is obvious that struct sample stores 3 different data types i.e. int a, char b and char c in one place under the hood of a single structure named a sample. The question which commonly arises here is, how this structure get stored in memory. The answer to this is, each data element present in a structure is allocated a different memory location of its size in memory. But all data members store at contiguous memory location as shown below: 

Let’s suppose integer variable takes 4 bytes of memory storage and it stores from address 200-203 and character variable takes 1 byte of memory. Hence, both character b and c store at address 205 and 205, respectively.

structure memory layout in C

Compilers add paddings bytes between structure elements to enhance CPU performance in cost of memory. It is beyond the topic of this topic to discuss structure padding, I will post a separate article on this. 

One important point to note here is that the starting address of a structure is a address of its first elements which is integer variable a in the above example.

Now if copy the following code and compile it, it will give you the size of struct “sample”. 

#include<stdio.h>
typedef struct  
{  
    int a;   
    char b;   
    char c;  
}sample;

int main(void) {
	printf("Size of structure is: %d\n", sizeof(sample));
	return 0;
}
Size of structure is: 8

Now let’s move to our actual question and see how to find size of struct without using the sizeof() operator.

Now, we create a pointer to struct and initize this pointer variable to NULL. Here, we have defined a pointer to a struct sample. If you don’t know about pointers, you can read this getting started guide:

A pointer ptr contains the address of a struct. As discussed earlier, the address of a structure is a address of its first element. But in this case, we assigned a address zero to ptr. Hence, the address of int a is 0, char a is 5 and char b is 5. Moreover, there will be two extra padding bytes. Therefore, the address of range for this structure will be from 0 – 7.

typedef struct  
{  
    int a;   // address 0
    char b; // address 4
    char c; //address 5 and two extra padding bytes
}sample;

sample *ptr=NULL;
How to find the Size of structure without sizeof() Operator

Now if we increment the pointer variable ptr, it will be increment by the size of the structure. Because ptr is a pointer to a structure.

ptr++; // points to address 8 
How to find the Size of structure without sizeof() Operator in c

Note: When we increment a pointer, it is increment by the size of data to which its points to.

If you are confused about pointer arithmetic, you can read this post:

Complete C code:

#include<stdio.h>
typedef struct  
{  
    int a;   
    char b;   
    char c;  
}sample;

int main(void) {
    sample *ptr=NULL;
    ptr++;
	printf("Size of structure is: %d\n", ptr);
	return 0;
}

Code output:

Size of structure is: 8

You may also like to read:

Leave a Comment