0

I am quite new to C and I am playing with some string operations. I have encountered quite a strange problem for me while returning a string from a function. My simple program is as follows:

int main(int argc, char* argv[])
{
    char text[] = "abAB";

    char* out = testString(text);

    printf("Result Text: %s", out);
    printf("\n");
}

char* testString(char* input) {
    char* text = copyString(input);

    return text;
}

The copyString function defines a simple operation to copy one string to another. It is as follows:

char* copyString(char* input) {
    char output[100];

    int index = 0;
    while (input[index] != '\0') {
        output[index] = input[index];
        index++;
    }

    output[index] = '\0';

    return output;
}

The problem is that while I am debugging the application, the string I am returning from a function seems to be OK (Visual Studio visualises it well enough) and when the printf line occurs, the string outputted on the stdout is something completely strange and unfamiliar - a smily face. Sadly, I can't post images yet in order to show you what I see in my console as output.

I am using Visual C++ Express 2010 as an IDE if this could be helpful.

garyzhu
  • 13
  • 5
  • 3
    `char output[100];` is a temporary variable, it will be destroyed as soon as `copyString` returns leaving you with a dangling pointer. – user657267 Sep 11 '15 at 06:20
  • That seems to be a reasonable argument. Well how do you suggest I proceed in order to create a string variable and return it afterwards then? – PreslavMihaylov Sep 11 '15 at 06:21
  • With posix a quick and dirty way is to use `strdup`, you'll want to avoid using it in production code however unless you can guarantee that its argument is correctly terminated. – user657267 Sep 11 '15 at 06:27

1 Answers1

0

You are returning a variable declared within a function, which will cease to exist outside the scope in which it is declared. Use a dynamically allocated char array and then return a pointer to it.

char* output = malloc(100 * sizeof(char));
...
return output ;

Note : You are assuming that input string is less than 100 characters. Instead of that, try passing the length of string as a parameter or use strlen. Your program will crash if input string has more than 99 characters.

Also as noted in comments, free the memory allocated when you are done using it.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
  • Do not forget to free this memory after you use that – kaitian521 Sep 11 '15 at 06:23
  • I am getting a compile error now which doesn't allow me to create that char arr using the way you specified. I am getting the following error: A value of type void* cannot be used to initialize an entity of type char* – PreslavMihaylov Sep 11 '15 at 06:26
  • Just use a cast i.e. `char* output = (char*)malloc(100 * sizeof(char));` – a_pradhan Sep 11 '15 at 06:29
  • I don't know what result your visual studio compiler gives, when I compile your code in a `gcc` (standard) environment, this is the warning: ` gcc play.c play.c: In function ‘copyStrin g’: play.c:12:5: warning: function returns address of local variable [-Wreturn-local-addr] return output; ^ ` Now, this warning clearly states, you want to return a `char*` holding the value of your copied string, but what you are returning is the address of a local variable, which will be destroyed as soon as it get out of scope. – vish4071 Sep 11 '15 at 06:30
  • So, your pointer becomes `dangling pointer`, ie. it is now pointing to any memory location, which is not under your control for now. It could have given you a segmentation fault (SIGSEGV) as well.
    What you need to do is declare your output as: `char *output = (char *)malloc(100 * sizeof(char));` and then do your operations. It should work fine then.
    – vish4071 Sep 11 '15 at 06:31
  • I can't format answer in comments !! – vish4071 Sep 11 '15 at 06:31