×

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

Related Topics

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Lexicographically Next Permutation in C++

In this tutorial, we'll look at how to use C++ to generate the lexicographically next permutation of a string. The lexicographically next permutation is the larger permutation. "ACB," for example,...

3 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.