Re: return a string



On 29 Apr 2005 14:04:10 -0700, underscore0x5f@xxxxxxxxx (Nascimento)
wrote:

>Hello,
>
>How to I do to return a string as a result of a function.
>I wrote the following function:
>
>char prt_tralha(int num)

Here, you're saying to return a character, not a string. You want
char * prt_tralha(int num)
where the returned value will be a pointer to a character array
(string).

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

You can't declare a variable size array. What you can do is
char *tralha;
tralha = malloc(num);
but remember to check the return value to make sure the malloc
succeeded. One possibility is
if (tralha == NULL)
return NULL;


Also, since you are allocating new memory, it needs to be freed at
some point. More below on this.
>
> tralha = "#";

You can't just assign strings. (Your first compiler error below.) You
can do
strcpy(tralha, "#");

> for( i = 0; i < num-1; i++ )
> strcpy(tralha, strcat(tralha,"#"));
>
This makes little sense, but I think I can guess what you're trying to
do. Make a string with num '#'s, right? How about
for( i = 0; i < num-1; i++ )
strcat(tralha, "#");

> return tralha;

This is OK now, since you've allocated new memory for tralha. In your
original, you returned the address of a local array, which will be
invalid as soon as you return from the function. This is why you got
the third compiler error. The second error was because you were
returning a pointer, when you told the compiler you wanted to return a
char.
>}
>
>And I really like to use it, thus:
>
>int main()
>{
> printf("%s \n", prt_tralha(5));

Now, we have the problem that prt_tralha has allocated memory which
needs to be freed. In your test program it doesn't matter, but in a
real program, every call to prt_tralha would allocate more memory, and
you could eventually run out. That's what we call a memory leak. One
way to fix this is

char *temp = prt_tralha(5);
if (temp != NULL)
{
printf("%s \n", temp);
free temp;
}
>
> return 0;
>}
>
>But when I compile it, gcc shows this message:

Some hints: Look up the definitions and try to find example uses of
every library function you use, such as strcpy, strcat, malloc, free
above. I referred to all your compiler diagnostics as "errors" on
purpose. Unless you know exactly why the compiler issued a warning,
and you know for certain that it's harmless, consider it an error and
fix it. There is hardly ever a good reason for ignoring compiler
warnings.

Get a good tutorial. I recommend "The C Programming Language" by
Kernighan and Ritchie, which is a good tutorial and reference.
>
>tmp.c: In function `prt_tralha':
>tmp.c:16: error: incompatible types in assignment
>tmp.c:20: warning: return makes integer from pointer without a cast
>tmp.c:20: warning: function returns address of local variable
>
>Thanks,
>
>Nascimento

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@xxxxxxx
.



Relevant Pages

  • Re: Segmentation fault
    ... Here you ask for a pointer to char. ... to a random position in memory. ... There's nothing else than a string the user could enter;-) ... to the use of scanf(). ...
    (comp.lang.c)
  • Re: Degenerate strcmp
    ... thing of searching through the memory at s1 and s2. ... pointers are passed to strcmp. ... actually a string. ... I think the subtle point is the following: a char * isn't actually the ...
    (comp.lang.c)
  • Re: Something wrong in my program
    ... what becomes of the memory block starting at this address is no ... our text buffer can contain 15 characters ... a string is a char array *terminated ...
    (comp.lang.c)
  • Re: Concat some string not ended...
    ... > i try to concat some string not ended ... What you're dealing here with are simple char arrays ... 'size' isn't compile-time constant and thus the length of the array ... a char pointer and than allocate enough memory. ...
    (comp.unix.programmer)
  • Re: char *s = "Google"
    ... The compiler may choose to store the string in a region of ... That memory may cause an signal to be raised ...
    (comp.lang.c)