I'm beginner with C and I am learning on my own. I am creating the following function:
char *foo(int x){
if(x < 0){
char a[1000];
char b = "blah";
x = x - 1;
char *c = foo(x);
strcpy(a, b);
strcat(a, c);
return a;
}
blah ...
}
I am basically trying to return an appended string, but I get the following error:
"error: function returns address of local variable", any suggestions, how to fix this?
PRACTICE:
I am currently teaching a friend of mine the basic concepts of C - and came up with this very simple and straight-forward (i hope so) code example which basically explains everything. Didn't want to hide it from you guys! :)
When compiling it, you get the [intended] warning:
...basically what we are discussing here!
running my example yields this output:
THEORY:
This has been answered very nicely by User @phoxis. Basically think about it this way: Everything inbetween { and } is local scope, thus by the C-Standard is "undefined" outside. By using malloc you take memory from the HEAP (programm scope) and not from the STACK (function scope) - thus its 'visible' from outside. The second correct way to do it is call-by-reference. Here you define the var inside the parent-scope, thus it is using the STACK (because the parent scope is the main()).
SUMMARY:
3 Ways to do it, One of them false. C is kind of to clumsy to just have a function return a dynamicaly sized String. Either you have to malloc and then free it, or you have to call-by-reference. Or use C++ ;)
This line:
Is no good - your lvalue needs to be a pointer.
Your code is also in danger of a stack overflow, since your recursion check isn't bounding the decreasing value of x.
Anyway, the actual error message you are getting is because
char a
is an automatic variable; the moment youreturn
it will cease to exist. You need something other than an automatic variable.a
is an array local to the function.Once the function returns it does not exist anymore and hence you should not return the address of a local variable.In other words the lifetime of
a
is within the scope({
,}
) of the function and if you return a pointer to it what you have is a pointer pointing to some memory which is not valid. Such variables are also called automatic variabels because their lifetime is automatically managed you do not need to manage it explicitly.Since you need to extend the variable to persist beyond the scope of the function you You need to allocate a array on heap and return a pointer to it.
This way the array
a
resides in memory untill you call afree()
on the same address.Do not forget to do so or you end up with a memory leak.
should be:
Neither malloc or call by reference are needed. You can declare a pointer within the function and set it to the string/array you'd like to return.
Using @Gewure's code as the basis:
works perfectly.
With a non-loop version of the code in the original question:
a
is defined locally in the function, and can't be used outside the function. If you want to return achar
array from the function, you'll need to allocate it dynamically:And at some point call
free
on the returned pointer.You should also see a warning at this line:
char b = "blah";
: you're trying to assign a string literal to achar
.