
Simple question about function return
Quote:
>There are two functions
>functionA(), functionB()
>functionA call functionB().
>And functionA want to get return value string type from functionB
>**************
>void functionA()
>{
>char *a;
>*a = functionB(1);
>Messagebox("test", a);
>}
>char functionB(int n_input)
>{
> char *b;
> if n_input =1
> b = "Yes";
> else
> b = "No";
>return *b;
>}
>***************************
>But a is only One byte
>Help me.
char *b; means that b is a pointer to a character. So when you return *b you
are returnining the single character to which b is pointing. I think what you
are trying to do is to return a pointer to a string of characters. To do this
you have to return the pointer itself. The compiler generates a pointer when
you put a sting into quotes. What you need is
char *functionB(int n_input)
{
char *b; /* b is a pointer to a character */
if n_input =1
b = "Yes";
else
b = "No";
return b;
Quote:
}
But you cannot do this
char *functionB(int n_input)
{
char b[100]; /* b is a pointer to a charcter also here. */
if n_input =1
strcpy(b, "Yes");
else
strcpy(b, "No");
return b;
Quote:
}
In this case the string b is on the stack, and does not exist anymore when you
have returned from b. The compiler puts literal strings in a separate place,
and what is compiled into the code is the address of that place. The way that c
implements arrays is that the expression &b[n] is exactly the same as b+n.
Roger Abbott,
RHA (Minisystems) Ltd.
http://www.angelfire.com/biz/rhaminisys
Windows shareware, DDE tools, Algol60