How to find sizeof array in C/C++ without using sizeof?

how to find size of array without using size of operator in c

A pointer is an amazing tool of c language we can do any task easily with the help of pointers. In my previous article, we have read that how to calculate the size of structure without using the sizeof() operator.

In this article, I am calculating the sizeof array without using the sizeof() operator. Calculating the size of an array in c without using the sizeof() operator seems to be difficult but with the help of pointers arithmetic, we can do it easily.

In c language when we increment or decrements the pointer then pointer point the next or previous memory location. The next or previous location depends on the pointer type. If the pfData is a pointer to the float then next location will be 4 bytes ahead of the current location.

size of structure in c

In below section, I am writing a program to find the total number of elements in the array (size of the array) without knowing the data type of elements.

Method 1

c sizeof array

Before understanding the above concept I want to remind the relation between array and pointer.

Let’s suppose aiData is an array of integers then &aiData[i] is the pointer to the integer and its value is the address of the ith element. We can also represent the statement in mathematical form.

aiData[i] = *(aiData + i);

*(aiData + i) = aiData[i];

(aiData + i) = &aiData[i];

If the value of i is 0.

(aiData + 0) = &aiData[0];

aiData = &aiData[0];
means that array name is the address of its first element.

sizeof array c++

Note: But when you put the ampersand(&) before the array name then its type change.It becomes a pointer to the array.

In the short, we can say.

aiData ====> Pointer to the first element of the array .

&aiData ====> Pointer to an array of 6 elements.

&aiData + 1 ====> Address of next memory block (Address ahead of 6 integers)

*(&aiData+ 1) ====> Dereferencing to *(&aiData + 1) gives the address of first element of second memory block.

*(&aiData+ 1) – aiData ====> Since *(&aiData + 1) points to the address ahead of 6 integers , the difference between two is 6.

sizeof array

Method 2

Create own sizeof operator using the macro and calculate the total number of elements in the array.


  1. This way of figuring out the size of an array only applies to statically allocated arrays. Pointers are even more interesting when using dynamically allocated memory.

Leave a Reply