4 Ways to Find the C++ Array Length

How to find array length in C++

One of the ways to get the length of an array is using the sizeof() operator in C++.

There are other ways that you may use to get the array length:

  • By using pointers hack
  • size() function in STL
  • By using the begin() and end() functions of Standard Library

Let us look at these ways with C++ programs of each in the section below.

Get the array length by sizeof() operator

The sizeof() operator returns the size of the type in bytes.

It actually does not return the size of the array directly.

As sizeof() returns the size of the type in bytes, you may get the array length by dividing the size of an array by the size of the data type it contains.

The example below makes it clear with a char type array:

#include <iostream>

using namespace std;




int main() {

//Declaring char type array

   char ch_array[] = {'a','e','i','o','u'};

   int arr_size = sizeof(ch_array) / sizeof(char);




  cout <<"Size of Array = " << arr_size;

  return 0;

}

Output:

CPP-array-size

You see, the program returned the array size as 5.

As such the char type takes one byte.

Let us now look at an example of int type array.

The example of sizeof() with int array

First have a look if we directly display the sizeof() function return result:

#include <iostream>

using namespace std;




int main() {

  int int_arr[] = {5, 10, 15, 20, 25, 30, 35};

  cout << sizeof(int_arr);




  return 0;

}

Output:

28

This is because sizeof() returns the size of type in bytes; so it multiplied 7 elements of array by 4. As such, the int type takes 4 bytes., so the result is 28.

Now let us look at how to get the correct result:

#include <iostream>

using namespace std;




int main() {

  //Declare and assign int array type

  int int_arr[] = {5, 10, 15, 20, 25, 30, 35};




  //Getting the array length

  int arr_size = sizeof(int_arr) / sizeof(int);




  //Display the array length

  cout <<"Length of array: " << arr_size;




  return 0;

}

Output:

CPP-array-size-int

You can see, our int array contains seven elements.

Second way: An example of using size() function in STL

Basically, the size() can be used for the length of any given container e.g. list, vector, string, and array as well. This size() function is defined in the standard library. An example with four arrays (including int and char) is shown below:

#include <iostream>

#include <array>

using namespace std;




int main() {

                //Creating four arrays

                array<int,0> array_1{};

                array<int,3> array_2{};

                array<char,5> array_3{'a','e','i','o','u'};

                array<int,7> array_4{0, 5, 15, 20, 25, 30, 35};







                cout<<"Array 1 length: "<<array_1.size()<<endl;

                cout<<"Array 2 length: "<<array_2.size()<<endl;

                cout<<"Array 3 length: "<<array_3.size()<<endl;

                cout<<"Array 4 length: "<<array_4.size()<<endl;




                return 0;

}

Output:

CPP-array-size

Second way – The example of using begin() and end functions to get array length

You may also use begin() and end() functions of C++ Standard library to get the length of an array. See the following C++ program for an idea:

#include<iostream>

using namespace std;

int main()

{

                //Creating four arrays

    int array_1[1] = {5};

    int array_2[3] = {};

    char array_3[] = {'a','e','i','o','u'};

    int array_4[10] = {0, 5, 15, 20, 25, 30, 35};




   //Displaying array length by end() and begin()

   cout<<"Array 1 Length : "<<end(array_1)-begin(array_1) <<"\n";

   cout<<"Array 2 Length : "<<end(array_2)-begin(array_2) <<"\n";

   cout<<"Array 3 Length : "<<end(array_3)-begin(array_3) <<"\n";

   cout<<"Array 4 Length : "<<end(array_4)-begin(array_4) <<"\n";




   return 0;

}

 

Output:

CPP-array-size-end-begi

The pointers hack to get array length example

You may get the length of an array by using pointers as well. This simple line of code can get the answer:

int size = *(&arr + 1) – arr;

Where expression *(&arr + 1) returns the address of the memory space after the last element in the specified array.

See this in a C++ program below:

#include <iostream>

using namespace std;

int main() {

   //Creating a char array

   char ch_array[] = {'a','e','i','o','u'};




   //Get array length by using pointer

   int arr_size = *(&ch_array + 1) - ch_array;




   //Display the result

   cout << "The char array length is: " << arr_size;

   return 0;

}

Output:

The char array length is: 5

An example with int type array

Similarly, you may get the array length of int or other types of array.

#include <iostream>

using namespace std;

int main() {

   //Creating int type array

   int int_arr[] = {5, 10, 15, 20, 25, 30, 35};




   //Get the size of array by using pointer

   int arr_size = *(&int_arr + 1) - int_arr;




   //Display result

   cout <<"Length of array: " << arr_size;

   return 0;

}

Output:

Length of array: 7