Simple question about function return 
Author Message
 Simple question about function return

HI
This is simple question

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);

Quote:
}

char functionB(int n_input)
{
 char *b;

 if n_input =1
    b = "Yes";
 else
    b = "No";

return *b;

Quote:
}

***************************
But a is only One byte
Help me.


Wed, 18 Apr 2001 03:00:00 GMT  
 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



Wed, 18 Apr 2001 03:00:00 GMT  
 Simple question about function return

Quote:

>HI
>This is simple question

>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.

Ignoring the obvious typos, you're returning a char, when you want to return
a char*. However, if functionB was to return a char*, it had better continue
to return a pointer to a string with static storage duration, such as a
string literal. Don't ever return a pointer to an auto variable, because
they cease to exist following the return. All this would be covered by any
good C book, and I'd recommend you follow that up by reading the C FAQ:

 http://www.eskimo.com/~scs/C-faq/top.html

--
Doug Harrison



Fri, 20 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question about signal()/pointers to functions that return pointers to functions

2. QUESTION: functions returning pointers to functions

3. a question on function returning a pointer to a function

4. Pointer to function returning pointer to function returning...

5. Simple C++ question: Using return values (by value)

6. simple malloc question in functions

7. Simple question: Member function templates in template classes under VC++ 5.0

8. Simple question about Pointers to Functions

9. Function Timing Simple Question

10. Question about returning values of functions

11. Functions Returning Question

12. newbie question: returning structs from functions

 

 
Powered by phpBB® Forum Software