0

I was just curious to know how to find the number of elements of an array of integers. For character arrays we can loop thorough the array till we reach the null character,but how can I do it for integer array?

#include <iostream.h>
void main()
{
       int a[] = {1,2,3,4};
       for ( k = 0 ; a[k] ; k++)
          cout<<k<<endl;
}

The above code counts from 0 to 8.

-A C++ noob with an open mind

  • 2
    If you have the actual array you can find out the size (in bytes) of the array by using `sizeof(array)`. However, as soon as it decays to a pointer (for example if you pass it to a function) then there's no way of knowing the size. The same goes with general `char` arrays, what you're thinking of are C-style strings which have a special terminator (the array itself may be longer than the actual string in it though). Also, you might want to learn about [`std::array`](http://en.cppreference.com/w/cpp/container/array). – Some programmer dude Dec 07 '14 at 13:42
  • Every array has '\0' stored at the very last index where elements end. Take a loop and look for '\0' value with a counter. You will get the number of elements stored(not the size of the array). array[10] = {1,2,3,4,5,'\0'}; Here the size is 10. but number of elements are 5 and i can get the number of element by just finding where '\0' is existing :) – mfs Dec 07 '14 at 13:50
  • Please read the question , i tried it and the number of elements turned out to be 8 –  Dec 07 '14 at 13:50

2 Answers2

1

A char array is terminated by 0 by convention. Such an array is called a C-style string, because it's used as a string of characters.

For integers, there is no termination value by convention, and you need to know the length by some other means. If it's your own array, store the length in a variable. If you receive the array from an API, there will be typically be a parameter receiving the length of the array, that you can use.

driis
  • 161,458
  • 45
  • 265
  • 341
0

If the array is a global, static, or automatic variable (int array[10];), then sizeof(array)/sizeof(array[0]) works.

If it is a dynamically allocated array (int* array = malloc(sizeof(int)*10);) or passed as a function argument (void f(int array[])), then you cannot find its size at run-time. You will have to store the size somewhere.

Note that sizeof(array)/sizeof(array[0]) compiles just fine even for the second case, but it will silently produce the wrong result.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Anshul Rai
  • 772
  • 7
  • 21
  • is there a way to calculate the number of elements without in built functions? –  Dec 07 '14 at 13:45
  • While this is technically correct, nobody should be using this approach to find the number of elements. Mainly because the answer given by this is the same as the value you put in when you first declared the array. Define array sizes as constants, and use those everywhere. Usually, the programmer would use another variable to keep track of the number of elements in an array. Increment this variable when you add a value, and decrement when you remove. An STL container such as a vector might be able to do what you are looking for. – shebang Dec 07 '14 at 15:24