Hi,
Is there any function in C++ to get the length of an array?? Like java has
array.getLength() ???
thanx n cheers
array length
Moderator: Board moderators
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.
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
Hi Yarin,
Thanx a lot. This solves a crucial problem of mine.
Thanx a lot. This solves a crucial problem of mine.