
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Size of Array in C/C++ Without Using sizeof
In C/C++, we usually use the sizeof operator to find the size of an array. However, once an array is passed to a function, its size is lost. So, how can we find the size of an array without using sizeof?
For example, consider this array:
int arr[] = {1, 2, 3, 4, 5};
Without using sizeof, we can use a loop to count the number of elements in the array.
In this article, we'll show you different methods to find the size of an array in C or C++ without using sizeof, with clear and simple examples.
Approaches to Find Array Size Without sizeof
We will be seeing different approaches through which we can find the size of an array without using sizeof.
- Using a Loop to Count Elements
- Using Pointers and Pointer Arithmetic
- Using a Sentinel Value
- Using a Wrapper Function for Static Arrays
- Using a C++ Template Function
- Self-made sizeof() Function
Using a Loop to Count Elements
One of the simplest ways to find the size of an array without sizeof is by iterating through the array using a loop and counting the elements manually.
Example
In this example, we initialize a counter variable and use a loop to go through the array. We increase the counter for each element and stop when we reach a specific value (in this case, 0) that marks the end of the array.
#include <stdio.h> int findArraySize(int arr[]) { int count = 0; while (arr[count] != '\0') { // Loop until null character is encountered count++; } return count; } int main() { int arr[] = {1, 2, 3, 4, 5, 0}; // Adding a sentinel value to mark the end int size = findArraySize(arr); printf("The size of the array is: %d\n", size); return 0; }
Below is the output of the code, which shows the size of the array.
The size of the array is: 5
Time Complexity: O(n), both the array traversal and size calculation loop through the array.
Space Complexity: O(1), it uses a constant amount of extra space, regardless of the input size.
Using Pointers and Pointer Arithmetic
This approach uses pointer arithmetic to find the difference between the start and end addresses of the array. By subtracting the start address from the end address, we get the number of elements in the array.
Example
In this example, we calculate the size by using pointers that point to the start and the end of the array, then subtract the start pointer from the end pointer to get the size.
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; int* start = arr; int* end = arr + sizeof(arr) / sizeof(arr[0]); int size = end - start; cout << "Size of array: " << size << endl; return 0; }
Below is the output of the code, which shows the size of the array.
Size of array: 5
Time Complexity: O(1), as the size calculation and pointer arithmetic are done in constant time.
Space Complexity: O(1), since only a constant amount of space is used for the pointers (start and end).
Using a Sentinel Value
In this approach, we use a sentinel value (like -1 or 0) placed at the end of the array to mark the end and count the elements until we reach it.
Example
In this example, we use the sentinel value -1 to mark the end of the array and count the elements before it by looping through the array until we reach the sentinel value.
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, -1}; // -1 as sentinel value int count = 0; // Loop to count elements until sentinel value is reached while(arr[count] != -1) { count++; } cout << "Size of array: " << count << endl; return 0; }
Below is the output of the code, which shows the size of the array excluding the sentinel value:
Size of array: 5
Time Complexity: O(n), due to the loop iterating through the array until the sentinel value is reached.
Space Complexity: O(1), as only a constant amount of space is used.
Using a Wrapper Function for Static Arrays
This method works with static arrays, where the size is known when we compile the program. We can wrap the array in a function and use its size as an argument.
Example
In this example, we use a macro called ARRAY_SIZE to calculate how many elements are in the array by dividing the total size of the array by the size of one element. This method works only when the array size is known at compile time.
#include <stdio.h> // Macro to calculate the size of the array #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) int main() { int arr[] = {1, 2, 3, 4, 5}; // Calculate the size using the macro int size = ARRAY_SIZE(arr); printf("The size of the array is: %d\n", size); return 0; }
Below is the output of the code, which shows the size of the array.
The size of the array is: 5
Time Complexity: O(1), because the size calculation is done in constant time.
Space Complexity: O(1), as it uses a fixed amount of space for the array and size variable.
Using a C++ Template Function
We can calculate the size of a static array (where the size is known at compile time) using a C++ template function. This method is flexible because template allow us to handle arrays of any type, making it more useful than using a wrapper function.
Example
In this example, we use a template function getSize to calculate the size of a static array at compile time. The function takes the array as an argument and returns its size.
#include <iostream> using namespace std; template <typename T, size_t N> size_t getSize(T(&arr)[N]) { // Return the size of the array using template argument return N; } int main() { int arr[] = {1, 2, 3, 4, 5}; cout << "The size of the array is: " << getSize(arr) << endl; return 0; }
Below is the output of the code, which shows the size of the array:
The size of the array is: 5
Time Complexity: O(1), as the size is determined at compile time.
Space Complexity: O(1), as only a constant amount of space is used.
Self-made sizeof() Function
In this approach, we create a custom macro that works like the sizeof operator, using pointer arithmetic to calculate the array size manually. This method offers more control and flexibility when sizeof isn't available or a custom solution is needed.
Example
In this example, we define a macro my_sizeof to calculate the array size using pointer arithmetic by subtracting memory addresses and dividing by the size of one element
#include <iostream> using namespace std; // Custom macro to calculate size of a variable #define my_sizeof(type) ((char*)(&type + 1) - (char*)(&type)) int main() { int arr[] = {1, 2, 3, 4, 5}; // Array initialization // Calculate array size using custom macro int size = my_sizeof(arr) / my_sizeof(arr[0]); cout << "Array size: " << size << endl; return 0; }
Below is the output of the code, which shows the size of the array.
Array size: 5
Time Complexity: O(1), the macro calculates the size in constant time.
Space Complexity: O(1), the program uses a constant amount of space.
Conclusion
In this article, we covered different ways to find the size of an array without using the sizeof operator. These methods are helpful when working with dynamic arrays or passing arrays to functions. Whether using pointer arithmetic, loops, or template functions, these methods offer flexible ways to calculate array size.