LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-22-2004, 01:31 AM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
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
 
Old 06-22-2004, 02:02 AM   #2
vbhatia_81
LQ Newbie
 
Registered: Jun 2004
Posts: 13

Rep: Reputation: 0
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 ];
}
 
Old 06-22-2004, 02:24 AM   #3
j_pooria
LQ Newbie
 
Registered: Jun 2004
Distribution: Fedora
Posts: 7

Rep: Reputation: 0
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!
 
Old 06-22-2004, 02:28 AM   #4
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
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?
 
Old 06-22-2004, 05:47 AM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
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() )

Last edited by keefaz; 06-22-2004 at 05:49 AM.
 
Old 06-22-2004, 05:51 AM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Why not use a std::vector in place of an array?
 
Old 06-22-2004, 06:38 AM   #7
j_pooria
LQ Newbie
 
Registered: Jun 2004
Distribution: Fedora
Posts: 7

Rep: Reputation: 0
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!
 
Old 06-22-2004, 09:44 AM   #8
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
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
 
Old 06-22-2004, 02:18 PM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 06-22-2004, 02:28 PM   #10
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
I believe I'll use vectors.... it seems I've been spoiled by java but C++ is so fun too!

Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: array size limit? bigfatdud Programming 14 01-27-2005 12:19 PM
Size of an array in C Hady Programming 6 04-06-2004 06:33 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
size of an array Longinus Programming 8 03-08-2004 02:48 PM
getting size of an array in c++ schatoor Programming 8 11-06-2003 06:19 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration