the 2.nd one is also working fine ... check it out .. http://www.ideone.com/ue96W
the given arguments are all correct .. bt since its returning a copy of it to the main .. it'll work fine ... On Mon, Jul 18, 2011 at 11:28 PM, sunny agrawal <[email protected]>wrote: > Yes thats right but a small but obvious correction which holds good till > you explicitly call free function or... > > > On Mon, Jul 18, 2011 at 10:49 PM, Swathi <[email protected]> wrote: > >> 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 <[email protected]> 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 [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> 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 [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Sunny Aggrawal > B-Tech IV year,CSI > Indian Institute Of Technology,Roorkee > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Shubham Maheshwari ShubZz O.o o.O enJoY ...!!! -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
