Find the Size of Array in C/C++ without using sizeof() function
We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of each element that is the data it is holding is in a continual fashion.
We generally use the pre-defined function the sizeof() to get the length or the size of an array we have declared. Sizeof() function is an underlying smallest unit of code which is being run in the background calculating the array size or the length of the array that we will pass through the sizeof() function. This article will focus on calculating the size of an array without using the sizeof() function in both C and C++ programming languages.
Declaring Arrays in C and C++
Here, we will see how to declare an array in C and C++programming:
Code in C
// writing C program to declare array
//and printing its output in the console
#include <stdio.h>
int main() {
//we will write code to initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array
int length = 5;
printf("Elements of the given array are : ");
//we will now be looping through the array by incrementing value of i
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
Elements of the given array are: 1 2 3 4 5
Code in C++
// C++ program to declare array
//and printing its output in the console
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
int n = 6;
//we will now be looping through the array by incrementing value of i
for(int i = 0;i<n;i++){
cout<<arr[i]<<endl;
}
return 0;
}
Output
1 2 3 4 5 6
C and C++ Code to Find the Size of an Array given by using sizeof() Function
We will find the size of array given by using the sizeof() function in C and C++ programs.
C code
//C code to find the length of an array without sizeof() function
#include<stdio.h>
int main()
{
int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2 };
int count = sizeof(a) / sizeof(int);
printf("There are %d no of elements in the array\n", count);
return 0;
}
Output
There are 11 no of elements in the array
C++ code
// C++ program to find size of
// an array
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
int size = sizeof(arr)/sizeof(arr[0]);
cout << size;
return 0;
}
Output
6
Finding the Size or Length of Array without using sizeof() Function in both C and C++
As we have seen, an approach in both C and C++ to find the size of an array or the length of an array using the pre-defined size of a function here is the second and even more exciting approach to finding the size of an array using the pointer hack approach. The pointer hack approach is pretty simple and also even very easy to use. As we know that the presence of the concept pointers is what differentiates C and C++ programming languages from other languages such as Python, Java etc., so here to find the length or the size of an array, we are using the pointer hack approach whose syntax and implementation of the code is as follows
Syntax of Pointer Hack Approach
//Assume an example array below
int array[] = {1,2,3,4,5,6,7,8,9}
//syntax for pointer hack approach
int size = *(&array + 1) - array; // &array returns a pointer
C code
//C code to find the length of an array without sizeof() function
#include<stdio.h>
int main()
{
int arr[] = {32, 422, 226, 444444, 56788, 219, 330, 32, 435, 65, 2, 23,
533336, 637, 3338, 3339, 30, 33, 32, 13, 34, 5, 6, 7, 8};
int size = *(&arr + 1) - arr;
printf("There are about %d number of elements in the array \n", size);
return 0;
}
Output
There are about 25 number of elements in the array
C++ code
//C++ code to find the length of an array without sizeof() function
#include <iostream>
using namespace std;
int main() {
int arr[] = {32, 422, 226, 444444, 56788, 219, 330, 32, 435, 65, 2, 23,
533336, 637, 3338, 3339, 30, 33, 32, 13, 34, 5, 6, 7, 8};
int s = *(&arr + 1) - arr;
cout << "Number of elements in array are "<< s;
}
Output
Number of elements in array is 25