How do I find the length of an array?

0 votes
asked Nov 5, 2010 by maxpm

Is there a way to find how many values an array has? Detecting whether or not I've reached the end of an array would also work.

18 Answers

0 votes
answered Nov 5, 2010 by oliver-charlesworth

If you mean a C-style array, then you can do something like:

int a[7];
std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;

This doesn't work on pointers, though, i.e. it won't work for either of the following:

int *p = new int[7];
std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;

or:

void func(int *p)
{
    std::cout << "Length of array = " << (sizeof(p)/sizeof(*p)) << std::endl;
}

int a[7];
func(a);

In C++, if you want this kind of behaviour, then you should be using a container class; probably std::vector.

0 votes
answered Nov 5, 2010 by prasoon-saurav

Is there a way to find how many values an array has?

Yes!

Try sizeof(array)/sizeof(array[0])

Detecting whether or not I've reached the end of an array would also work.

I dont see any way for this unless your array is an array of characters (i.e string).

P.S : In C++ always use std::vector. There are several inbuilt functions and an extended functionality.

0 votes
answered Nov 5, 2010 by mahlerfive

Doing sizeof( myArray ) will get you the total number of bytes allocated for that array. You can then find out the number of elements in the array by dividing by the size of one element in the array: sizeof( myArray[0] )

0 votes
answered Nov 5, 2010 by eq

std::vector has a method size() which returns the number of elements in the vector.

(Yes, this is tongue-in-cheek answer)

0 votes
answered Nov 6, 2013 by motti

As other's said you can use the sizeof(arr)/sizeof(*arr) but this will give you the wrong answer for pointer types that aren't arrays.

template<class T, size_t N>
constexpr size_t size(T (&)[N]) { return N; }

This has the nice property of failing to compile for non array types (visual studio has _countof which does this). The constexpr makes this a compile time expression so it doesn't have any drawbacks over the macro (at least none I know of).

You can also consider using std::array from C++11 which exposes its length with no overhead over a native C array.

0 votes
answered Jan 3, 2014 by das-newb

Just a thought, but just decided to create a counter variable and store the array size in position [0]. I deleted most of the code I had in the function but you'll see after exiting the loop, prime[0] is assigned the final value of 'a'. I tried using vectors but VS Express 2013 didn't like that very much. Also make note that 'a' starts at one to avoid overwriting [0] and it's initialized in the beginning to avoid errors. I'm no expert, just thought I'd share.

int prime[] = {0};
int primes(int x, int y){
    using namespace std; int a = 1;
    for (int i = x; i <= y; i++){prime[a] = i; a++; }
    prime[0] = a; return 0;
}
0 votes
answered Jan 21, 2014 by miksiii

Lets say you have an global array declared at the top of the page

int global[] = { 1, 2, 3, 4 };

To find out how many elements are there (in c++) in the array type the following code:

sizeof(global) / 4;

The sizeof(NAME_OF_ARRAY) / 4 will give you back the number of elements for the given array name.

0 votes
answered Nov 2, 2014 by metal

There's also the TR1/C++11/C++17 way (see it Live on Coliru):

const std::string s[3] = { "1"s, "2"s, "3"s };
constexpr auto n       = std::extent<   decltype(s) >::value; // From <type_traits>
constexpr auto n2      = std::extent_v< decltype(s) >;        // C++17 shorthand

const auto     a    = std::array{ "1"s, "2"s, "3"s };   // C++17 class template arg deduction -- http://en.cppreference.com/w/cpp/language/class_template_argument_deduction
constexpr auto size = std::tuple_size_v< decltype(a) >;

std::cout << n << " " << n2 << " " << size << "\n"; // Prints 3 3 3
0 votes
answered Nov 5, 2014 by jukkap
#include <iostream>

int main ()
{
    using namespace std;
    int arr[] = {2, 7, 1, 111};
    auto array_length = end(arr) - begin(arr);
    cout << "Length of array: " << array_length << endl;
}
0 votes
answered Nov 13, 2014 by mr-foots

Instead of using the built in array function aka:

 int x[2] = {0,1,2};

you should use the array class and the array template. Try:

#include <array>
array<type_of_the_array, number_of_elements_in_the_array> Name_of_Array = {};

so now if you want to find the length of the array all you have to do use the size function in the array class.

Name_of_Array.size();

and that should return the length of elements in the array.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...