2

I am having problem handling string array in C++.I tried below two methods.Still not able to resolve the problem...Here it goes : When I use :

string* fun(string* a,string*b) 
{
    string c[];
    return c; /* c is string array type*/ 
} 

it returns first string stored in string array c.I want whole string array to be returned. When I used:

vector<string> fun(vector<string> a,vector<string> b){
    vector<string> c;
    return c;
}

still,i got some errors.
can you help me know where is the problem in both cases. What modifications are required to obtain the desired result.. How can I handle string array in C++. Thanx in advance !!

3
  • 2
    what errors did you get when using a vector<string>? Commented Jun 13, 2012 at 5:35
  • 4
    You shouldn't be returning a pointer to a local variable on the stack that will just go out of scope when the function ends. Commented Jun 13, 2012 at 5:38
  • The second method is returning a vector<string>, so what errors did you get and what outcome were you expecting? Commented Jun 13, 2012 at 5:47

5 Answers 5

1

In the first version, you are returning a pointer to a local variable (your array), which will not exists any longer when you leave the scope. You need to create your array on the heap, e.g. with malloc or new. If you allocate it manually, don't forget to deallocate it.

In the second version, you are returning a copy of the vector declared in your function (if you modify the strings in the returned vector, they'll not be modified in a et b). You are creating an empty vector and not adding anything in it, so it'll not contains any string, though.

Sign up to request clarification or add additional context in comments.

2 Comments

His first code snippet is returning a pointer to a local (temporary) array. That is definitely not correct.
the first code is wrong since it is returning a pointer to something that won't exist after the function exits.
1

If you've got to return an object more complex than string you should pass to your function a link to vector (in your code) and fill it with values. That's a fast and right method.

vector<string> fun(vector<string> a,vector<string> b, vector<string>& result){
  res.push_back("one");
  res.push_back("two");
  res.push_back("three");
}

8 Comments

C++11 move semantics eliminate the overhead of returning a copy of a local variable.
I think there is no talking about c++11 here ;)
I disagree, what you propose could actually be less efficient than returning by value, given return value optimization (RVO).
@AlanirAlonedaw, It's still good to mention if speed is an issue. I prefer using the return value. And yes, RVO would most likely come into play.
Even without C++11, a decent compiler and NRVO (named return value optimization) can make returning a vector inexpensive. VJ's second code snippet above looks fine to me.
|
0

In C/C++ while returning a string from function a local buffer if returned will not work.

The returned pointer should be a static buffer(like static string c[]) or pointer to a buffer passed in by the caller function (like string *fun(string *a, string *b, string *c) ) or pointer to a memory obtained using malloc/new but not local array.

Comments

0

In your first snippet, you could try initializing with new to allocate the space yourself.

string* func() {
    string* c = new string[3];
    c[0] = "Hello";
    c[1] = "World";
    c[2] = "<3";
    return c;
}

in main:

m = func();
for(int i = 0; i < 3; i++)
{
    cout << m[i] << endl;
}

That should prevent it from losing scope once the function ends. Don't forget to deallocate the space. Note you are returning a pointer to an array, not an array. Also, I had no problem running your second snippet of code. Please always share the errors you are getting.

2 Comments

i wanted whole string array to be returned..anyways thnx u 4 an alternative !!
I don't think you can return an entire array. If I remember correctly, you only return pointers to an array's starting point, and handle them appropriately. That's all an array is anyway, it is allocated space with an internal pointer.
0

You can't return arrays directly in C++(it inherits this from C). The way around it is to stuff it into a struct.

struct returned_array
{
    string result[100];
};
returned_array fun()
{
    returned_array result;
    result.result[0] = "whatever";
    return result;
}

I just picked an arbitrary size 100 for the example. If you want to base the return size on the size of an array that gets passed in, you can add a template parameter:

template<int N>
struct returned_array
{
    string result[N];
};
template<int N>
returned_array<N> fun(string (&)[N])
{
    returned_array<N> result;
    result.result[0] = "whatever";
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.