Nuno Emanuel F. Carvalho wrote:

>  When we want to return a (char*) type from a function what should we do ?

Either have the caller allocate the storage (as is the case for the
string.h functions), or use malloc().

In certain cases, you can return a pointer to a static buffer. 
However, such functions are prone to incorrect usage, and aren't
suitable for use in multithreaded programs. In general, this approach
should be avoided.

> /*  This case (1) */
> main()
> {
>  
>  printf("\n %s", foo() ) ;
>  return 0 ;
> }
> 
> 
> /* This case (2)*/
> main()
> {
>   char *exp ;
> 
>   exp = foo() ;
> 
>   printf("\n %s", exp);
>   free(exp); 
>   
>   return(0) ;
> }
> 
>  Which is the most correct ? The second !?

In this case, it doesn't matter. All memory will be freed when the
program terminates. In general, an application should free() the data
once it has finished with it.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to