tags:

views:

107

answers:

5

How do I return an array of strings from a recursive function?

For example::

char ** jumble( char *jumbStr)//reccurring function
{
   char *finalJumble[100];

   ...code goes here...call jumble again..code goes here

   return finalJumble;
} 

Thanks in advance.

+5  A: 

In C, you cannot return a string from a function. You can only return a pointer to a string. Therefore, you have to pass the string you want returned as a parameter to the function (DO NOT use global variables, or function local static variables) as follows:

char *func(char *string, size_t stringSize) {
    /* Fill the string as wanted */
    return string;
}

If you want to return an array of strings, this is even more complex, above all if the size of the array varies. The best IMHO could be to return all the strings in the same string, concatenating the strings in the string buffer, and an empty string as marker for the last string.

char *string = "foo\0bar\0foobar\0";

Your current implementation is not correct as it returns a pointer to variables that are defined in the local function scope.

(If you really do C++, then return an std::vector<std::string>.)

Didier Trosset
Thanks for ur reply, Didier.I am actually doing it in c. not c++. Sorry for the wrong tag..Can u suggest a different approach.
vj01
Applying all three ideas as needed. Thanks all..
vj01
If you want to terminate with empty string, the example probably should be `char *string = "foo\0bar\0foobar\0\0";`
che
@che: The compiler already adds this last '\0'. No need to add it!
Didier Trosset
@Didier Trosset: True, thanks for clarification.
che
+2  A: 

You don't :-)

Seriously, your code will create a copy of the finalJumble array on every iteration and you don't want that I believe. And as noted elsewhere finalJumble will go out of scope ... it will sometimes work but other times that memory will be reclaimed and the application will crash.

So you'd generate the jumble array outside the jumble method:

void jumble_client( char *jumbStr)
    char *finalJumble[100];

     jumble(finalJuble, jumbStr);

     ... use finalJumble ...
} 

void jumble( char **jumble, char *jumbStr)
{
   ...code goes here...call jumble again..code goes here
}

And of course you'd use the stl datatypes instead of char arrays and you might want to examine whether it might be sensible to write a jumble class that has the finalJumble data as a member. But all that is a little further down the road. Nevertheless once you got the original problem solved try to find out how to do that to learn more.

froh42
Thanks, Applying all three ideas as needed. Thanks all..
vj01
actually I used this suggestion too and it worked very well. I am writing in clang . So the class thing would not work..
vj01
Yeah, originally this article was tagged C++. But I'm glad to help
froh42
+2  A: 

Your implementation is not correct since you are passing a pointer to a local variable that will go out of scope rather quickly and then you are left with a null pointer and eventually a crash.

If you still want to continue this approach, then pass by reference (&) an array of characters to that function and stop recursing once you have reached the desired end point. Once you are finished, you should have the 'jumbled' characters you need.

0A0D
Thanks, Applying all three ideas as needed. Thanks all..
vj01
+1  A: 

I would pass a vector of strings as a parameter, by reference. You can always use the return value for error checking.

typedef std::vector<std::string> TJumbleVector;

int jumble(char* jumbStr, TJumbleVector& finalJumble) //reccurring function 
{
    int err = 0;; // error checking

     ...code goes here...call jumble again..code goes here
     // finalJumble.push_back(aGivenString);

     return err; 
}  

If you want to do it in C, you can keep track of the number of strings, do a malloc at the last recursive call, and fill the array after each recursive call. You should keep in mind that the caller should free the allocated memory. Another option is that the caller does a first call to see how much space he needs for the array, then does the malloc, and the call to jumble:

char** jumble(char* jumbStr)
{
    return recursiveJumble(jumbStr, 0);
}

char** recursiveJumble(char* jumbStr, unsigned int numberOfElements)
{
    char** ret = NULL;
    if (/*baseCase*/)
    {
        ret = (char**) malloc(numberOfElements * sizeof(char*));
    }
    else
    {
        ret = jumble(/*restOfJumbStr*/, numberOfElements+1);
        ret[numberOfElements] = /*aGivenString*/;
    }
    return ret;
}
rturrado
Thanks. I am using the C lang.. Thanks.. Learned a lot today..
vj01
Thanks for ur help
vj01
A: 

Hi all! Thanks for ur wonderful replies. I have one more question. Can I use a bit array for this purpose by setting the bit of the position of the characters in the word, or something like that?

vj01