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 and float size is 4 byte then the next location will be 4 bytes ahead of the current location.

size of structure in c

 

 

Let us see some example programs where we are calculating the number of elements in the array (size of the array) without knowing the data type of elements. 

Example program 1:

#include <stdio.h>

int main(int argc, char *argv[])
{

    int iTotalElement = 0 ;
    int  aiData[] = {10, 20, 30, 40, 50, 60};

    //Calculate numbers of elements using pointer arithmetic
    iTotalElement = *(&aiData + 1) - aiData;

    printf("Number of element = %d",iTotalElement);

    return 0;
}

Output: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];

It means an 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

Example program 2:

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

#include <stdio.h>
// User created size of operator
#define SIZEOF(Var) ((char*)(&Var + 1) -(char*)&Var)

int main(int argc, char *argv[])
{
    int iTotalElement = 0 ;
    int  aiData[] = {10, 20, 30, 40, 50, 60};

    iTotalElement = SIZEOF(aiData)/SIZEOF(aiData[0]);

    printf("Number of element = %d",iTotalElement);

    return 0;
}

Output:

c sizeof array

 

 

 

Recommended Articles for you,

20 comments

  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.

  2. You are Dereferencing to *(&aiData + 1) gives the address of first element of second memory block and how it accessing unused memory.

    1. It will work. Like the below program.

      #include

      int main(int argc, char *argv[]) {

      int *p = NULL;

      printf(“%d\n”,*&p);

      return 0;
      }

      (&aiData + 1) behave like a pointer to the array.

Leave a Reply

Your email address will not be published. Required fields are marked *