J
jammie_linux
Hi,
Please look at the following code. In my opinion, the value of "char
*str" in the main function should not change even after calling the
function "funct" and that's beacuse the "char *str" in the function
funct is local to the function funct and therefore it's value will be
meaningless when the function funct returns. But still the value of
"char *str" in the main function is changing after calling the function
funct. Can anybody help me?
#include <stdio.h>
char *funct()
{
char *str = "This is a local string";
return str;
}
int main(void)
{
char *str;
str = "This is a test";
printf("%s\n",str);
str = funct();
printf("%s\n",str);
return 0;
}
Please look at the following code. In my opinion, the value of "char
*str" in the main function should not change even after calling the
function "funct" and that's beacuse the "char *str" in the function
funct is local to the function funct and therefore it's value will be
meaningless when the function funct returns. But still the value of
"char *str" in the main function is changing after calling the function
funct. Can anybody help me?
#include <stdio.h>
char *funct()
{
char *str = "This is a local string";
return str;
}
int main(void)
{
char *str;
str = "This is a test";
printf("%s\n",str);
str = funct();
printf("%s\n",str);
return 0;
}