Skip to content
Related Articles

Related Articles

Do Not Use sizeof For Array Parameters in C

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 01 Jul, 2022
View Discussion
Improve Article
Save Article

Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers. Consider the below program.  

C




// C Program to demonstrate incorrect usage of sizeof() for
// arrays
#include <stdio.h>
 
void fun(int arr[])
{
    int i;
 
    // sizeof should not be used here to get number
    //  of elements in array
    int arr_size = sizeof(arr) / sizeof(arr[0]);
 
    for (i = 0; i < arr_size; i++) {
        arr[i] = i;
    }
    // executed two times only
}
 
// Driver Code
int main()
{
    int i;
    int arr[4] = { 0, 0, 0, 0 };
    fun(arr);
 
    // use of sizeof is fine here
    for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
        printf(" %d ", arr[i]);
 
    getchar();
    return 0;
}

Explanation: This code generates an error as the function fun() receives an array parameter ‘arr[]’ and tries to find out the number of elements in arr[] using sizeof operator. 
In C, array parameters are treated as pointers (See this for details). So, the expression sizeof(arr)/sizeof(arr[0]) becomes sizeof(int *)/sizeof(int) which results in 2 (size of int* is 8 bytes because its an pointer and pointer occupies the 8 bytes of memory and int is 4) and the for loop inside fun() is executed only two times irrespective of the size of the array. Therefore, sizeof should not be used to get a number of elements in such cases. 

Solution: 

1) Using a separate parameter: A separate parameter of datatype size_t for array size or length should be passed to the fun().  size_t is an unsigned integer type of at least 16 bits. So, the corrected program will be:

C




// C Program to demonstrate correct usage of sizeof() for
// arrays
#include <stdio.h>
 
void fun(int arr[], size_t arr_size)
{
    int i;
    for (i = 0; i < arr_size; i++) {
        arr[i] = i;
    }
}
 
// Driver Code
int main()
{
    int i;
    int arr[] = { 0, 0, 0, 0 };
    size_t n = sizeof(arr) / sizeof(arr[0]);
    fun(arr, n);
 
    printf("The size of the array is: %ld", n);
    printf("\nThe elements are:\n");
    for (i = 0; i < n; i++)
        printf(" %d ", arr[i]);
 
    getchar();
    return 0;
}

Output

The size of the array is: 4
The elements are:
 0  1  2  3 

2) Using Macros: We can even define Macros using #define to find the size of arrays, as shown below,

C




// C Program to demonstrate usage of macros to find the size
// of arrays
#include <stdio.h>
 
#define SIZEOF(arr) sizeof(arr) / sizeof(*arr)
 
void fun(int arr[], size_t arr_size)
{
    int i;
    for (i = 0; i < arr_size; i++) {
        arr[i] = i;
    }
}
 
// Driver Code
int main()
{
    int i;
    int arr[] = { 0, 0, 0, 0, 0 };
    size_t n = SIZEOF(arr);
    fun(arr, n);
 
    printf("The size of the array is: %ld", n);
    printf("\nThe elements are:\n");
    for (i = 0; i < n; i++)
        printf(" %d ", arr[i]);
 
    return 0;
}

Output

The size of the array is: 5
The elements are:
 0  1  2  3  4 

3) Using Pointer arithmetic: We can use (&arr)[1] – arr to find the size of the array. Here, arr points to the first element of the array and has the type as int*. And, &arr has the type as int*[n] and points to the entire array. Hence their difference is equivalent to the size of the array.

C




// C Program to demonstrate usage of pointer arithmetic to
// find the size of arrays
#include <stdio.h>
 
void fun(int arr[], size_t arr_size)
{
    int i;
    for (i = 0; i < arr_size; i++) {
        arr[i] = i;
    }
}
 
// Driver Code
int main()
{
    int i;
    int arr[] = { 0, 0, 0, 0, 0 };
    size_t n = (&arr)[1] - arr;
    fun(arr, n);
 
    printf("The size of the array is: %ld", n);
    printf("\nThe elements are:\n");
    for (i = 0; i < n; i++)
        printf(" %d ", arr[i]);
 
    return 0;
}

Output

The size of the array is: 5
The elements are:
 0  1  2  3  4 

Please write comments if you find anything incorrect in the above article or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!