C Programming

# How to Determine Size of an Array

Array is a linear collection of elements. To calculate the size of array we can use sizeof() operator. With the help of sizeof() operator we can easily find out the size of an array. Except the sizeof() operator we can also use pointer to calculate the size of array.

## Basic Principle of sizeof Operator to Calculate the Size of Array

Memory required (in bytes) = sizeof (datatype) * sizeof array.

Example: int a ;

Then, sizeof( datatype ) = sizeof( int ) = 4 bytes

Sizeof array = 10 .

So, memory required = ( 4 * 10 ) bytes = 40 bytes

## Programming Example 1:

# include <stdio.h>

# include <conio.h>

int main()

{

int arr [] = { 1, 2 , 3 , 4 , 5 } ;

int i ;

int size = sizeof(arr) / sizeof (arr) ; // size of array.

printf ( “ size of array = %d\n”, size ) ;

printf ( “ The array elements are :) ;

for( i = 0; i < size ; i++)

{

printf (“ arr [ %d ] = %d\n”, i , arr[i]) ;

}

}

Output: Size of array is 5 Explanation: Here, we declare an array named arr[] and assign some values. They are 1, 2, 3, 4, 5. If we want to determine the size of array, means how many elements present in the array, we have to write the calculation with the help of sizeof operator.

Sizeof( arr [] ) / sizeof (arr) ;

Here, the size of arr[] is 5 and each integer takes memory 4 bytes.

So, the total memory is consumed = ( 5 * 4 ) bytes.

= 20 bytes.

Sizeof (arr ) means here the elements are integer. So, it takes memory 4 bytes.

So, the size of the array = ( 20 / 4 ) bytes = 5 bytes.

If we take character array or string instead of integer array, we can explain what happened in the next program.

## Programming Example 2:

# include <stdio.h>

# include <conio.h>

int main()

{

char arr [] = { a , b , c ,d , e } ;

int i ;

int size = sizeof (arr) / sizeof (arr [ 0 ] ) ; // size of array

printf ( “ size of array = %d \n ”, size ) ; .

printf (“ The array elements are :) ;

for ( i = 0; i < size ; i++)

{

printf ( “ arr [ %d ] = %c \n”, i , arr [ i ] ) ;

}

}

Output: Size of array is 5 Explanation: Here, we declare an array named arr[] and assign some values.They are {‘a’, ‘ b ‘, ‘ c ‘, ‘ d ‘, ‘ e ‘}. If we want to determine the size of array, means how many elements present in the array, we have to write the calculation with the help of sizeof() operator.

Sizeof( arr[] ) / sizeof( arr ) ;

Here, the size of arr [] is 5 and each character takes memory 2 bytes.

So, the total memory is consumed = ( 5 * 2 ) bytes.

= 10 bytes.

sizeof ( arr  ) means here the elements are character. So, it takes memory 2 bytes.

So, the size of the array = (10 / 2 ) bytes = 5 bytes.

If we take float array instead of character array, we can explain what happened in the next program.

## Programming Example 3:

# include <stdio.h>

# include <conio.h>

int main()

{

char arr [] = { 1.5 , 2.5 , 3.5 , 4.5 , 5.5 } ;

int size = sizeof(arr) / sizeof ( arr [ 0 ]) ; //size of array

printf ( “size of array = %d \n”, size ) ;

printf ( “ array elements :) ;

for ( int i = 0 ; i < size ; i++ )

{

printf ( “ arr[ %d ]=%f \n ”, i ,arr [ i ] ) ;

}

}

Output: Size of array is 5 Explanation: Here, we declare an array named arr[] and assign some values.They are {1.5, 2.5, 3.5, 4.5, 5.5}. If we want to determine the size of array, means how many elements present in the array, we have to write calculation with the help of sizeof() operator.

Sizeof(arr[]) / sizeof(arr) ;

Here, the size of arr[] is 5 and each float takes memory 8 bytes.

So, the total memory is consumed = (5 * 8) bytes.

= 40 bytes.

Sizeof (arr ) means here the elements are float. So, it takes memory 8 bytes.

So, the size of the array = (40 / 8) bytes = 5 bytes.

## Calculate the Size of Array Using Pointer

Another method to determine the size of array is by using pointer.

## Programming Example 4:

int main()

int main()
{
int arr [] = { 1 ,2 , 3 , 4 , 5 };
int size = * ( &arr + 1) – arr ; // declaring the size variable using pointer.
printf(Number of elements are arr[] is %d”, size);
return 0 ;
}

Explanation: Here, we calculate the size of the array using pointer.

int size = * ( &arr + 1) – arr ;

The above line helps us to calculate the size of the array. Here, arr means the base address of the array or address of the first index of the array.

* ( &arr + 1)

If we subtract the address value of the array from its base address, then we get the size of each block in the array. Then, we can easily find out the size of the array by counting the total no of inputs that we have given to that particular array.

Output: ## Conclusion

Clearly, it is shown that with the help of sizeof() operator pointer, we can easily calculate the length of array or the size of the array. Mainly, sizeof() operator is responsible to calculate the size of the array but additionally pointer can also support to determine the size of the array passively. 