Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?
Here is a simple code, where in 3 different functions [ localStrPtr, localIntPtr, localCharPtr] return a pointer to their local variables [string, integer, char] in their respective functions.
CODE:
#include <stdio.h>
char* localStrPtr (char*);
int* localIntPtr (int, int);
char* localCharPtr (char);
main()
{
int *pInt;
char *pChar;
printf( "localStrPtr = %s\n", localStrPtr("abcd") );
pInt = (int*) localIntPtr(3, 5);
printf( "localIntPtr = %d\n", *pInt );
pChar = (char*) localCharPtr('y');
printf( "localCharPtr = %c\n", *pChar );
}
char* localStrPtr(char* argu)
{
char str[20];
// char* str = (char*) malloc (20);
strcpy (str, argu);
return str;
}
int* localIntPtr (int argu1, int argu2)
{
int local;
local = argu1 + argu2;
return (&local);
}
char* localCharPtr (char argu)
{
char local;
local = argu;
return (&local);
}
COMPILE LOG:
stringManip.c: In function `localStrPtr':
stringManip.c:27: warning: function returns address of local variable
stringManip.c: In function `localIntPtr':
stringManip.c:34: warning: function returns address of local variable
stringManip.c: In function `localCharPtr':
stringManip.c:41: warning: function returns address of local variable
RUN LOG:
localStrPtr =
localIntPtr = 8
localCharPtr = y
As you can see in the log file, localStrPtr returns "some garbage", whereas localIntPtr and localCharPtr return "expected" values.
But, in the function localStrPtr, if I change [ "char str[20]" -to-> "char* str = (char*) malloc (20)" ], localStrPtr returns the string "abcd" correctly. Here is the RUN LOG, once the above change is made.
NEW RUN LOG:
localStrPtr = abcd
localIntPtr = 8
localCharPtr = y
QUESTIONS:
In functions localIntPtr and localCharPtr, contents of the returned local variable addresses WORKED, but for the function localStrPtr, correct value is returned "only" with malloc, but will not with local char str[20]. Why doesn't it work with str[20] ?
Why do we see in the COMPILE LOG, the lines below for all the 3 functions ?
- stringManip.c:27: warning: function returns address of local variable
- stringManip.c:34: warning: function returns address of local variable
- stringManip.c:41: warning: function returns address of local variable