calc. number of elements in array

I need help in calculating the number of elements stored in an array. For example if i have something like,

1
2
int one[] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int two[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};


then how do i calculate the number of elements in the arrays, considering that both the arrays will have the same number of elements at all times, but the number might vary.

tyler
The sizeof() function returns the number of bytes occupied by a variable or array. If you know that, then you just need to divide by the number of bytes that each element of the array occupies.

[code=c++]
int num_elements = sizeof( array ) / sizeof( array[0] );
[/code]
However, std::vector<int> should be preferred. It has a size()-function returning the number of elements. As soon as array to pointer decay ocurres (which will happen really fast), your method won't work anymore.
@ mahlerfive
The sizeof() function returns the number of bytes occupied by a variable or array. If you know that, then you just need to divide by the number of bytes that each element of the array occupies.

int num_elements = sizeof( array ) / sizeof( array[0] );


i tried this, but sizeof(array) always comes out to be 4, being an integer type, whereas it should come out to be 13 in the above example. its not really calculating the number of elements in the array.

tyler
Try this :

int num_elements_in_one = sizeof(one)/sizeof(int);
It returns the size of a pointer, which happens to be 4 bytes on your system. The problem is, that int[] should be a seperate type, however it is implicitly converted ("decays") to an int* pointing to the first element of the array. So you (i.e., your compiler) cannot really tell the difference between C-style arrays and pointers (well, it can in some situations, but in these you know the size anyways). Thus, don't use them - use std::vector<int> instead.
There are a number of other STL containers, and you should choose the one most suited to yuor application (IE what manipulations of the list you are going to do).
See http://www.cplusplus.com/reference/stl/ for more info.
mahlerfive's solution is correct, and it is the only way to learn the size of an array[]. The caveat is, as exception pointed out, that it only works in the array's declaration scope. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using std::cout;

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

int counta()
  {
  return sizeof( a ) / sizeof( a[ 0 ] );  // works, since a[] is an array
  }

int countb( int b[] )
  {
  return sizeof( b ) / sizeof( b[ 0 ] );  // fails, since b[] is a pointer
  }

int main()
  {
  cout << "The size of a is " << counta() << endl;
  cout << "The size of b is " << countb( a ) << endl;
  }

There are actually several posts in the Articles section of the forum about this very topic: Pointer is not Array.

In counta(), the compiler knows the complete type of the global variable a --which includes its size.

In countb(), the compiler knows nothing about the pointer it is given (in argument lists int b[] is the same as int* b), so it cannot tell us anything about the complete type of its referrant.



I want to emphasize my agreement about using STL containers. They really do make everything easier.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int _a[] = { 1, 2, 3, 4, 5 };
vector <int> a( _a, _a + (sizeof( _a ) / sizeof( _a[ 0 ] )) );

int main( int argc, char* argv[] )
  {
  vector <string> args( argv, argv +argc );

  cout << "The array a[] = ";
  for (unsigned n = 0; n < a.size(); n++)
    cout << a[ n ] << ' ';
  cout << endl;

  cout << "The command line arguments:\n";
  for (unsigned n = 0; n < args.size(); n++)
    cout << n << ": " << args[ n ] << endl;

  return 0;
  }


Hope this helps.
Topic archived. No new replies allowed.