![]() |
VOOZH | about |
In C++, generally, we use the sizeof() operator to find the size of arrays. But there are also some other ways using which we can find the size of an array. In this article, we will discuss some methods to determine the array size in C++ without using sizeof() operator.
Given an array (you don't know the type of elements in the array), find the total number of elements in the array without using the sizeof() operator. The C++ Course provides practical methods to achieve this, enhancing your understanding of array handling. So, we can use the methods mentioned below:
The following solution is concise when compared to the other solution. The number of elements in an array A can be found using the expression:
// &arr returns a pointer
int size = *(&arr + 1) - arr;
Here the pointer arithmetic does its part. We don't need to explicitly convert each of the locations to character pointers.
- &arr - Pointer to an array of 6 elements. [See this for difference between &arr and arr]
- (&arr + 1) - After adding 1 to the address of array of 6 integers, the pointer will point to the memory location immediately after the end of the array.
- *(&arr + 1) - Same address as (&arr + 1), but type of pointer is "int *".
- *(&arr + 1) - arr - Since *(&arr + 1) points to the address 6 integers ahead of arr, the difference between two is 6.
Number of elements in arr[] is 6
We can define a macro that calculates the size of an array based on its type and the number of elements.
#define array_size(arr) (sizeof(arr) / sizeof(*(arr)))
Dividing the total size of the array by the size of a single element gives the number of elements in the array.
Number of elements in arr[] is 6
Using custom user-defined sizeof function which can provide the functionality same as sizeof( ).
Number of elements in arr[] is 6
To know more about the method refer to Implement our own sizeof.
We can use a template function to find the size of an array.
Number of elements in arr[] is 6
The array_size() function is a template function that takes two parameters:
The parameter T (&arr)[N] means the function accepts a reference to any type and any size of array. When array_size function is called with the array name as parameter, T is deduced as int and N is deduced as 6.
We can add a special value to the end of the array to indicate the end of the array, and then loop through the array until you find the sentinel value. This method is commonly used in string manipulation.
Output
Enter a value for element 0 (or -1 to stop): 7
Enter a value for element 1 (or -1 to stop): 5
Enter a value for element 2 (or -1 to stop): -1
Size of the array: 2
We can define a class or struct that contains an array as a member variable, and then define a member function that returns the size of the array. This method is useful if we need to pass the array and its size as a single parameter to a function.
Output
Enter a value for element 0 (or -1 to stop): 4
Enter a value for element 1 (or -1 to stop): 9
Enter a value for element 2 (or -1 to stop): 1
Enter a value for element 3 (or -1 to stop): -1
Size of the array: 3