Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return string array in C++ function

Tags:

I am new to C++. For a school project I need to make a function which will be able to return a string array.

Currently I have this in my header:

Config.h

string[] getVehicles(void); 

Config.cpp

string[] Config::getVehicles(){ string test[5]; test[0] = "test0"; test[1] = "test1"; test[2] = "test2"; test[3] = "test3"; test[4] = "test4";  return test;} 

Obviously this does not work but that's the idea of what I am trying to do. In Java this would be the way to do it. I've tried googling my problem but I didn't come across any answers that were clear to be honest.

like image 499
sn0ep Avatar asked Sep 23 '11 10:09

sn0ep


People also ask

Can I return an array from a function in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can I return a string from a function in C?

Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid.


2 Answers

Maybe it is better to use a vector in this case, but this is not a correct answer for the question. The reason why it doesn't work is that the variable test just exists in the scope of your function. So you have to manage the memory on your own. Here is an example:

string* getNames() {  string* names = new string[3];  names[0] = "Simon";  names[1] = "Peter";  names[2] = "Dave";    return names; } 

In this case you return a pointer of the position in the heap. All the memory in the heap has to free manually. So it is now your work to delete the memory, if you don't need it anymore:

delete[] names; 
like image 121
Simon P Avatar answered Oct 14 '22 22:10

Simon P


In C++ you don't use an array, but a std::vector instance. Arrays in C++ must have a compile-time fixed length while std::vector instances can change their length at runtime.

std::vector<std::string> Config::getVehicles() {     std::vector<std::string> test(5);     test[0] = "test0";     test[1] = "test1";     test[2] = "test2";     test[3] = "test3";     test[4] = "test4";     return test; } 

std::vector can also grow dynamically, so in a C++ program you will find more often something like

std::vector<std::string> Config::getVehicles() {     std::vector<std::string> test; // Empty on creation     test.push_back("test0"); // Adds an element     test.push_back("test1");     test.push_back("test2");     test.push_back("test3");     test.push_back("test4");     return test; } 

Allocating dynamically an array of std::string is technically possible but a terrible idea in C++ (for example C++ doesn't provide the garbage collector that Java has).

If you want to program in C++ then grab a good C++ book and read it cover to cover first... writing Java code in C++ is a recipe for a disaster because the languages, despite the superficial braces similarity, are very very different in many fundamental ways.

like image 40
6502 Avatar answered Oct 14 '22 20:10

6502