size of arrays
Hi to all
I have a quick question. I am going to fill an array with some inpuit of type char using member function cin.getline. After I fill an array ,I would like to do "for" loop so I need to know what is curently the size of an array. Is it possible. Looks like stupid question because I initialized the array that contains 250 elements (and what I know so far I can't chang ethe size of array, isn't right?). But if I would enter let sey 25 characters. Is the size going to change?Is it possible. If so how may I got the current size of the array. If not what is the best choice to fill an array (maybe vector ?) ,tht will let me know the current size of array
Thank you
mauro21pl
Junior Poster in Training
66 posts since Jan 2007
Reputation Points: 20
Solved Threads: 0
there is a function in c++ called strlen() which tells you the length of an array... if not, you can use a while loop with a counter that exits when array[i]=='\0'
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
>Is it possible.
When it comes to C++, if you ask this question the answer will nearly aways be yes. You can do damn near anything with C++.
>If so how may I got the current size of the array.
You're reading a C-style string, and C-style strings always end with a '\0' character. So if you loop until you find one, you know the size of the array:
int i;
for ( i = 0; i < 250; i++ ) {
if ( array[i] == '\0' )
break;
}
cout<<"The array is "<< i <<" characters long\n";
The strlen function from does this for you.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> there is a function in c++ called strlen() which tells you the length of an array
That would only be for char arrays, not any array.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Got it ).Thanks a lot guys. You are very helpfull. Appreciate it
mauro21pl
Junior Poster in Training
66 posts since Jan 2007
Reputation Points: 20
Solved Threads: 0
note : i searched google with "c++ length of array" and hit this thread, so dont tell me why i post to this thread although it is old.
to find the length of an array :
#include <iostream>
using namespace std;
int myArray[] = {5,6,7,8,9};
int lengthOfArray = sizeof(myArray) / sizeof(int);
char myArray2[] = {'a','b','c','d'};
int lengthOfArray2 = sizeof(myArray2) / sizeof(char);
int main()
{
cout << lengthOfArray << endl;
cout << lengthOfArray2 << endl;
return 0;
}
output is :
5
4
To conclude:
The generic solution to array length is :
int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
>int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);
While correct in and of itself, this doesn't answer the original question at all. It only works for actual arrays (as opposed to simulated arrays using pointers), so it's somewhat restricted in that you can't use it on "array" function parameters. Further, it always returns the full capacity of the array, not the current number of valid items, which was what the OP wanted to know.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401