You create a pointer, allocate memory to that pointer,
set the data, then return the pointer.  The pointer
itself loses scope and is lost, but the memory allocated
remains.  You then have to make sure a function somewhere
else frees the memory allocated.


~Patrick


> -----Original Message-----
> From: Nuno Emanuel F. Carvalho [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, February 23, 1999 11:38 AM
> To: [EMAIL PROTECTED]
> Subject: (char*)Function
> 
> 
> Hi,
> 
>  When we want to return a (char*) type from a function what 
> should we do ?
> 
>  Should be done allocation for the variable and after that 
> return those
> variable !?
>  As we shouldn't(?) free up that variable within the function 
> what should
> we do ? 
> 
> 
> char *foo ( )
> {
>   char *xpto ;
> 
>   xpto = malloc(30*sizeof(char)) ;
> 
>   . . . .
> 
>   return xpto ;
> }
> 
> 
> 
> /*  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 !?
>  
>  Thanks.
> 
>  Best regards,
>    Nuno Carvalho
> 
> 
> иииииииииииииииииииииииииииииииии
>    Nuno Emanuel F. Carvalho
>  Dep. Informatics Engineering
>     University of Coimbra
> 
>   PGP key available at finger
> иииииииииииииииииииииииииииииииии
> 
> 

Reply via email to