Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
c++
length

How to find the length of an ​array in C++

Educative Answers Team

There are several ways to compute the length of an array in C++.

svg viewer

Using sizeof()

One way​ to find the length of an array is to divide the size of the array by the size of each element (in bytes).

The built-in sizeof() operator is used for this purpose (shown in the code snippet below):

#include <iostream>
using namespace std;

int main() {
  int arr[] = {10,20,30,40,50,60};
  int arrSize = sizeof(arr)/sizeof(arr[0]);
  cout << "The size of the array is: " << arrSize;
  return 0;
}

Using pointer arithmetic

Since we have a pointer at the start of the array, the length of the​ array can be calculated if we manage to find out the address where the array ends. This is done as follows:

int arrSize = *(&arr + 1) - arr;

Let’s break down the above line of code:

  • (&arr + 1) points to the memory address right after the end of the array.
  • *(&arr + 1) simply casts the above address to an int *.
  • Subtracting the address of the start of the array, from the address of the end of the array,​ gives the length of the array.

Here’s how it’s implemented:

#include <iostream>
using namespace std;

int main() {
  int arr[] = {10,20,30,40,50,60};
  int arrSize = *(&arr + 1) - arr;
  cout << "The length of the array is: " << arrSize;
  return 0;
}

RELATED TAGS

array
c++
length
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring