In first 1, you are using malloc() so the memory will be allocated from heap
which holds good till end of your program.. In second case, the memory will
be allocated on the stack.. so once the function is exited, your stack data
will be released...

On Mon, Jul 18, 2011 at 9:49 PM, rj7 <r4ra...@gmail.com> wrote:

> Can someone pls explain the difference between these two especially in
> terms of scope... one example i have come across is
>        1.
>        char *xxx(int n)
>        {
>        char *retbuf = malloc(25);
>        if(retbuf == NULL)
>                return NULL;
>        sprintf(retbuf, "%d", n);
>        return retbuf;
>        }
>
>        2.
>        char *xxx(int n)
>        {
>        char retbuf[25];
>        sprintf(retbuf, "%d", n);
>        return retbuf;
>        }
>
> First one is correct and the second one is wrong... why does the scope
> of retbuf extends to the calling function...
> am just a beginner so ignore the stupidity(if any).....
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to