Page 1 of 1
array length
Posted: Thu Sep 12, 2002 8:38 am
by 17933PN
Hi,
Is there any function in C++ to get the length of an array?? Like java has
array.getLength() ???
thanx n cheers
Posted: Sun Sep 15, 2002 1:05 am
by Yarin
If you're using a plain C array, like this:
int a[100];
you can use sizeof(a)/sizeof(int) to get the number of elements in a. However, the size of the array is not bound to the variable, so if you send the array as a parameter to a function, you can't use sizeof anymore (because an array is just a pointer).
But since this is the C++ forum, you should use STL vector if you want such a facililty:
#include <vector>
vector<int> a(100);
cout << a.size() << endl;
Since a is an object, the size of it is known when using it as a parameter in a function etc.
Thanx Yarin
Posted: Sun Sep 15, 2002 6:48 am
by 17933PN
Hi Yarin,
Thanx a lot. This solves a crucial problem of mine.