C++ program to find size of array without using sizeof

Arrays are a fundamental part of computer programming. They are data structures that store multiple values in a single container. The size of an array is an important property that determines how many elements can be stored in it. In C++, we can use the sizeof operator to find the size of an array. But what if we need to find the size of an array without using the sizeof operator?

Well, the solution is quite simple. We can use a pointer to find the size of an array. Pointers are variables that store memory addresses. We can use a pointer to traverse the array and find its end. Here is how it can be done:

#include 
using namespace std;

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int size = 0;

while(*ptr) {
size++;
ptr++;
}

cout << "Size of the array: " << size << endl;

return 0;
}

The above code creates an array of integers and stores its starting address in a pointer variable. The pointer is then used to traverse the array until it reaches the end. The number of iterations is stored in the variable 'size', which is then printed to the console.

Submit Your Programming Assignment Details