Thread: [Dev-C++] how to pass a string array to another function
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Brad <nyc...@gm...> - 2012-09-17 11:40:41
|
My situation is this, I have a string array that I am trying to get the data to another function but have issues with the return type conflicting with either the function definition or the array data being passed. std::array Array[2] std::string f() { array[0]=ted array[1]=bundy return Array[2]; }}; void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << Array[n] << endl; } int main () { printarray(); return 0; } Brad |
From: Brad <nyc...@gm...> - 2012-09-17 12:40:48
|
That's the problem, my function is not in main() and this is where the errors get weird is when I pass the same arguments in a function outside of main. Brad From: bibigiu [mailto:bi...@al...] Sent: Monday, September 17, 2012 8:04 AM To: Brad Subject: Re: [Dev-C++] how to pass a string array to another function Il 17/09/2012 13:40, Brad ha scritto: My situation is this, I have a string array that I am trying to get the data to another function but have issues with the return type conflicting with either the function definition or the array data being passed. std::array Array[2] std::string f() { array[0]=ted array[1]=bundy return Array[2]; }}; void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << Array[n] << endl; } int main () { printarray(); return 0; } try this : #include <iostream> using namespace std; void PrintArray(std::string str[],int length) { for(int a=0;a<length;a++) { cout<< str[a].c_str() << endl; } } int main() { std::string str[2]; str[0] = "hello"; str[1] = "bye"; PrintArray(str,2); return 0; } ---------------------------------------------------------------------------- -- Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
From: Brad <nyc...@gm...> - 2012-09-17 21:34:04
|
My data is almost in its function but I am having difficulties with the final step. Goal: To get the array data into a textbox window on line 406 and 407 of -> http://pastie.org/4742333 But I am getting this error http://pastie.org/4742347 and I have tried a few different versions of passing the argument but no success. The array data is generated by my case statements starting on line 79 on http://pastie.org/4742337 in my main() <following earlier recommendations> This last screw is giving me fits. :( Brad From: bibigiu [mailto:bi...@al...] Sent: Monday, September 17, 2012 8:04 AM To: Brad Subject: Re: [Dev-C++] how to pass a string array to another function Il 17/09/2012 13:40, Brad ha scritto: My situation is this, I have a string array that I am trying to get the data to another function but have issues with the return type conflicting with either the function definition or the array data being passed. std::array Array[2] std::string f() { array[0]=ted array[1]=bundy return Array[2]; }}; void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << Array[n] << endl; } int main () { printarray(); return 0; } try this : #include <iostream> using namespace std; void PrintArray(std::string str[],int length) { for(int a=0;a<length;a++) { cout<< str[a].c_str() << endl; } } int main() { std::string str[2]; str[0] = "hello"; str[1] = "bye"; PrintArray(str,2); return 0; } ---------------------------------------------------------------------------- -- Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
From: Brad <nyc...@gm...> - 2012-09-18 01:45:34
|
My data is almost in its function but I am having difficulties with the final step. Goal: To get the array data into a textbox window on line 406 and 407 of -> http://pastie.org/4742333 But I am getting this error http://pastie.org/4742347 and I have tried a few different versions of passing the argument but no success. Brad From: bibigiu [mailto:bi...@al...] Sent: Monday, September 17, 2012 11:30 AM To: Brad Subject: Re: [Dev-C++] how to pass a string array to another function Il 17/09/2012 14:40, Brad ha scritto: That's the problem, my function is not in main() and this is where the errors get weird is when I pass the same arguments in a function outside of main. Brad From: bibigiu [mailto:bi...@al...] Sent: Monday, September 17, 2012 8:04 AM To: Brad Subject: Re: [Dev-C++] how to pass a string array to another function Il 17/09/2012 13:40, Brad ha scritto: My situation is this, I have a string array that I am trying to get the data to another function but have issues with the return type conflicting with either the function definition or the array data being passed. std::array Array[2] std::string f() { array[0]=ted array[1]=bundy return Array[2]; }}; void printarray (int arg[], int length) { for (int n=0; n<length; n++) cout << Array[n] << endl; } int main () { printarray(); return 0; } try this : #include <iostream> using namespace std; void PrintArray(std::string str[],int length) { for(int a=0;a<length;a++) { cout<< str[a].c_str() << endl; } } int main() { std::string str[2]; str[0] = "hello"; str[1] = "bye"; PrintArray(str,2); return 0; } ---------------------------------------------------------------------------- -- Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users ops... try this void PrintArray(std::string *str,int length) { for(int a=0;a<length;a++) { cout<< str[a].c_str() << endl; } } string* CreateStrArray(int size) { std::string *str = new std::string[size]; for(int a=0;a<size;a++) str[a] = "name"; return str; } int main( ) { std::string *str = CreateStrArray(2);// you must use pointer allocation with new(on the heap) for return object from a function... local variable created in the function are destroyed when return from the function!!!! PrintArray(str,2); return 0; } is that what you want? |