Re: return a string
- From: Alan Balmer <albalmer@xxxxxxx>
- Date: Fri, 29 Apr 2005 15:00:52 -0700
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
.
- Follow-Ups:
- Re: return a string
- From: Keith Thompson
- Re: return a string
- Prev by Date: Re: return a string
- Next by Date: Re: main(int argc, char *argv[])
- Previous by thread: Re: return a string
- Next by thread: Re: return a string
- Index(es):
Relevant Pages
|