size of arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

size of arrays

 
0
  #1
Jul 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: size of arrays

 
0
  #2
Jul 10th, 2007
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'
Last edited by Nichito; Jul 10th, 2007 at 10:56 pm.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: size of arrays

 
0
  #3
Jul 11th, 2007
Originally Posted by mauro21pl View Post
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
The best choice would be a std::string - which is a container specifically designed for holding characters (Far more appropriate to the task than a vector). You can use a std::string with getline() too - eg,
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string str;
  7. std::getline(std::cin, str);
  8. std::cout << str << " length: " << str.length();
  9. }
Last edited by Bench; Jul 11th, 2007 at 8:07 am. Reason: changed [CODE] to [CODE=CPP]
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: size of arrays

 
0
  #4
Jul 11th, 2007
>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:
  1. int i;
  2.  
  3. for ( i = 0; i < 250; i++ ) {
  4. if ( array[i] == '\0' )
  5. break;
  6. }
  7.  
  8. cout<<"The array is "<< i <<" characters long\n";
The strlen function from <cstring> does this for you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: size of arrays

 
0
  #5
Jul 11th, 2007
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 67
Reputation: mauro21pl is an unknown quantity at this point 
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: size of arrays

 
0
  #6
Jul 11th, 2007
Got it ).Thanks a lot guys. You are very helpfull. Appreciate it
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: size of arrays

 
0
  #7
Feb 6th, 2009
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 :

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int myArray[] = {5,6,7,8,9};
  5.  
  6. int lengthOfArray = sizeof(myArray) / sizeof(int);
  7.  
  8. char myArray2[] = {'a','b','c','d'};
  9.  
  10. int lengthOfArray2 = sizeof(myArray2) / sizeof(char);
  11.  
  12. int main()
  13. {
  14. cout << lengthOfArray << endl;
  15. cout << lengthOfArray2 << endl;
  16. return 0;
  17. }

output is :
5
4

To conclude:

The generic solution to array length is :

int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,730
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: size of arrays

 
1
  #8
Feb 6th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC