Re: return a string




Nascimento wrote:
> Hello,
>
> How to I do to return a string as a result of a function.
> I wrote the following function:
>

There are two ways to "return" a string from a function. One is to
simply return it...

> char prt_tralha(int num)

I.E. char * ptr_tralha(int num)

Here we return a pointer to char, or string. The problem with this is
the return value can't be an automatic variable, so you'll need to work
with malloc.

Another method is to make the string part of the argument list. This is
probably prefered, since the memory management takes place outside of
the function and is (arguably) easier.

I.E. char ptr_tralha(int num, char *ret)

> {
> int i;
> char tralha[num];

Assuming the later method, tralha would simply be the argument to this
function, and not declared here.

>
> tralha = "#";
> for( i = 0; i < num-1; i++ )
> strcpy(tralha, strcat(tralha,"#"));
>
> return tralha;

There would be no need to "return" tralha here, since it was passed in
via pointer in the argument list.

> }
>
> And I really like to use it, thus:
>
> int main()
> {
> printf("%s \n", prt_tralha(5));

try this instead:

char tralha[5];
ptr_tralha(5,tralha);
printf("%s \n", tralha);

>
> return 0;
> }
>
> But when I compile it, gcc shows this message:
>
> tmp.c: In function `prt_tralha':
> tmp.c:16: error: incompatible types in assignment

Well, of course. The function is (was) returning a char, but you were
trying to use it as if it were a char *.

> tmp.c:20: warning: return makes integer from pointer without a cast
> tmp.c:20: warning: function returns address of local variable
>
> Thanks,

Hope this helps. I'm sure others will have suggestions as well.

-Jason
>
> Nascimento

.



Relevant Pages

  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)