array length

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
17933PN
New poster
Posts: 3
Joined: Wed Sep 11, 2002 7:59 pm

array length

Post by 17933PN »

Hi,
Is there any function in C++ to get the length of an array?? Like java has
array.getLength() ???

thanx n cheers
Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post 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.
17933PN
New poster
Posts: 3
Joined: Wed Sep 11, 2002 7:59 pm

Thanx Yarin

Post by 17933PN »

Hi Yarin,
Thanx a lot. This solves a crucial problem of mine.
Post Reply

Return to “C++”