Find the size of an array in C using sizeof operator and pointer arithmetic
This post provides an overview of some of the available alternatives to find the size of an array in C.
1. sizeof operator
The standard way is to use the sizeof operator to find the size of a C-style array. The sizeof operator on an array returns the total memory occupied by the array in bytes. To determine the total number of the array elements, the trick is to divide the total memory occupied by the array by the size of each element. This is demonstrated below in C:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main(void) { int arr[] = { 1, 2, 3, 4, 5 }; size_t n = sizeof(arr)/sizeof(arr[0]); printf("The size of the array is %d", n); return 0; } |
For simplicity and to improve code readability, it is suggested to construct a MACRO out of it, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> #define SIZEOF(a) sizeof(a)/sizeof(*a) int main(void) { int arr[] = { 1, 2, 3, 4, 5 }; size_t n = SIZEOF(arr); printf("The size of the array is %d", n); return 0; } |
We know that an array decays into a pointer when passed to a function as an argument regardless of whether the parameter is declared as int[] or not. So, above approach works only with the static arrays but fails on the dynamically allocated arrays and function parameters as they both involve pointers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <stdlib.h> #define N 5 // array decays into a pointer size_t getSize(int *arr) { size_t n = sizeof(arr)/sizeof(arr[0]); return n; } int main(void) { int *a = malloc(N * sizeof(int)); printf("%zu\n", sizeof(a)/sizeof(a[0])); // this won't work int b[] = { 1, 2, 3, 4, 5 }; printf("%zu\n", getSize(b)); // this won't work return 0; } |
2. Using pointer arithmetic
The trick is to use the expression (&arr)[1] - arr to get the array arr size. Both arr and &arr points to the same memory location, but they both have different types.
arrhas the typeint*and decays into a pointer to the first element of the array. Hence, any knowledge about the size of the array is gone.&arrresults in a pointer of typeint (*)[n], i.e., a pointer to an array ofnints. So,&arrpoints to the entire array and*(&arr + 1)(or&arr)[1]) points to the next byte after the array.
This works because of the way pointer arithmetic works in C. We know that a pointer to int is advanced by sizeof(int) when incrementing by 1. Similarly, a pointer to int[n] is advanced by sizeof(int[n]), which is the size of the entire array.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> int main(void) { int arr[] = { 1, 2, 3, 4, 5 }; size_t n = (&arr)[1] - arr; printf("The size of the array is %d", n); return 0; } |
That’s all about finding the size of an array in C using sizeof operator and pointer arithmetic.
Read More:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)