LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to find out an array size on c++ (https://www.linuxquestions.org/questions/programming-9/how-to-find-out-an-array-size-on-c-196252/)

poeta_boy 06-22-2004 12:31 AM

how to find out an array size on c++
 
Hello:

I'm a java programmer and I also (agh!) use C#. The thing is that now I'm on a project using C++ and I have come up with a quite silly, yet tricky question... how to find out the length of the array in C++?

lets say, I receive an array as a parameter in a method:

void method(string array[]);

and I wanna traverse it as with a for loop, normally in java I would:

for(int i = 0; i < array.length; i++);

but there's no length for arrays in c++ is there? I'm particulary interested on arrays of strings....

Any help? thanks for your time!

Poeta

vbhatia_81 06-22-2004 01:02 AM

i dont know about java as i have not worked on it but if your array is of char type
eg char name[20];
you can find the size of the array using string lib funxtion
int len = strlen(name);
it will return the size of the array as integer

then you can do

for(i=0; i < strlen(name) ; i++)
{
name[ i ];
}

j_pooria 06-22-2004 01:24 AM

Dear Poeta

As you know arrays are objects in Java and they have a property(.length) which gives you the length of the array. If you are going to use arrays in a wide range in your future programs, I suggest you define a class for arrays and make a method name ,for example, "add_element" to add anew element to you array.Then you can increase a private variable (e.g. int len) in this method and so you always have the length of your array. To avoid defining a class for arrays of each data type, you may define members of the array in your class "void" so it can be used to save any data type.The following function is an example of the method I said,for an array of integers:
void myarray::add_element(int x)
{
len++;
a[len]=x;//a is private int a[];
}

and then you can easily get the length of your array with:
int myarray::length()
{
return len;
}

for example:

myarray ary;
//.....
for(i=0;i<ary.length();i++)
{//....
}

but if you want a quick way to find out the length of an string, it's better to define a rule for your arrays,for example at first assign ""(null string to all members of your string array, so when it is passed to a function,the function assumes that where array finishes is the first "".Then you can find the length of your array with a simple while():
void a(string str[])
{
int count =0;
while(strcmp(str[count],"")!=0)//str[count] != ""
count ++;

so then count will have the length of str;

I hope I could help;
Good Luck!

poeta_boy 06-22-2004 01:28 AM

hi

thanks! I'm using an array of strings, included in the library <string> lets say:

string myArray[].

I can't do strlen(myArray) because it says:

"none of the two overloads can convert parameter 1 from type 'class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >[] '

now I can't really tell what that means but I can infere it's something that says that this function isn't defined for string objects.

any ideas?

keefaz 06-22-2004 04:47 AM

Just to be sure, you want know the size of an array of strings or the number of chars in a string ?

(for number of chars in a string, it is easy : int charNumber = myString.length() )

dakensta 06-22-2004 04:51 AM

Why not use a std::vector in place of an array?

j_pooria 06-22-2004 05:38 AM

Dear Poeta

strlen() function can return the length of an string but you passed "an array of strings" to it ;so the function can't understand what you want.

I suggest you use my first opinion,if you like & have no problem with it.

Good Luck!

poeta_boy 06-22-2004 08:44 AM

hey
yep yep, I'd like to know the size of an array of strings, as I receive an array of strings as parameter and I want to traverse it.... is it posible?

perhaps I can get a work around with your proposal Mr J_Pooria.... and perhaps I could also use vectors or even create a template class... if there's no direct way to find out the array length I might actually combine these two sugestions, since a new class containing an array and a private atribute len wouldn't let me dinamically change the size would it?

I was thinking about creating a class with a vector inside, where I can add and eliminate elements dinamically and then as accessor method make it return an array (with now the length as a atribute).

Just thinking if nobody have had this problem or why isn't there a solution already provided by standard libraries hehehe

aluser 06-22-2004 01:18 PM

The C way to get the length of an array passed as a parameter is either

* Pass the length as another parameter
* Use a sentinel value (often NULL) to mark the end of the array.

To be C++ish about it you would use some object instead of an array, perhaps <vector>. For a traditional array, the language really does not store information about its length anywhere; you have to do it yourself.

poeta_boy 06-22-2004 01:28 PM

I believe I'll use vectors.... it seems I've been spoiled by java ;) but C++ is so fun too!

Thanks


All times are GMT -5. The time now is 11:12 PM.