| | |
size of arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2007
Posts: 67
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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
CPP Syntax (Toggle Plain Text)
#include <iostream> #include <string> int main() { std::string str; std::getline(std::cin, str); std::cout << str << " length: " << str.length(); }
Last edited by Bench; Jul 11th, 2007 at 8:07 am. Reason: changed [CODE] to [CODE=CPP]
¿umop apisdn upside down? >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:
The strlen function from <cstring> does this for you.
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:
C++ Syntax (Toggle Plain Text)
int i; for ( i = 0; i < 250; i++ ) { if ( array[i] == '\0' ) break; } cout<<"The array is "<< i <<" characters long\n";
I'm here to prove you wrong.
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
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 :
output is :
5
4
To conclude:
The generic solution to array length is :
int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);
to find the length of an array :
C++ Syntax (Toggle Plain Text)
#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);
Due to lack of freedom of speech, i no longer post on this website.
>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.
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.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Huge Dynamic arrays in QB45, BC7 (Legacy and Other Languages)
- help in a very hard C question (C)
- loop to create arrays when reading a file (Java)
- Quicksort/Insertion sort Hyrbid? (C)
- Enhancing a Text based RPG (C++)
Other Threads in the C++ Forum
- Previous Thread: struct and function pointer member
- Next Thread: C++ Program that Displays Initials
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






