X
Xiaoshen Li
Dear All:
I am learning C and doing a program. This program hopes to use one
function getbuf() to return two char * type. One through return
statement. Another through the argument.
The program is following:
char *getbuf(char* pC);
int main()
{
char *b, *r;
b=getbuf(r);
printf("In main, b: %s \n", b);
printf("In main, r: %s \n", r);
return 0;
}
char* getbuf(char* pC)
{
static char buff[]="good morning";
pC ="good afternoon";
return (char*) buff;
}
However, after calling the function getbuf(), pointer b gets the value
"good morning" correctly. But pointer r cannot get the value "good
afternoon" from the function getbuf(). I thought by plugging pointer r
as the argument, r becomes the same pointer as pC in the function
getbuf(). And setting pC's value inside getbuf() will give r the same
value. But r never get the value "good afternoon".
Could you kindly help me out? This has driven me crazy. Thank you very much.
I am learning C and doing a program. This program hopes to use one
function getbuf() to return two char * type. One through return
statement. Another through the argument.
The program is following:
char *getbuf(char* pC);
int main()
{
char *b, *r;
b=getbuf(r);
printf("In main, b: %s \n", b);
printf("In main, r: %s \n", r);
return 0;
}
char* getbuf(char* pC)
{
static char buff[]="good morning";
pC ="good afternoon";
return (char*) buff;
}
However, after calling the function getbuf(), pointer b gets the value
"good morning" correctly. But pointer r cannot get the value "good
afternoon" from the function getbuf(). I thought by plugging pointer r
as the argument, r becomes the same pointer as pC in the function
getbuf(). And setting pC's value inside getbuf() will give r the same
value. But r never get the value "good afternoon".
Could you kindly help me out? This has driven me crazy. Thank you very much.