   why it works !!
  why it is that  fun2 does not work .. while myfunc() do not work ..in myfunc too .. we are returning the address of a local variable which will be destroyed when the function returns ??char* fun2(){    char buf[20];    strcpy(buf,"string123");    return buf;}char *myfunc(){char *temp = "string123";return temp;}regradsy2jsave   
  myfunc returns a string which is in a constant place in memory,fun2 copies the string into buf which at the end of the function goes out of scope. buff is allocated on the stack for use in this function. When the routine returns you pass back the adress on the stack , but area of memory will / can be overwritten. Thus its is destroyed.A "cheap" way around this is to change the declaration of buf to  static buf[20]; this has the effect of allocating it in memory and not on the stack.;  In fun2 a locally-scoped array is created on the stack and a string is copied into it. The function returns a pointer to the first element in that array. However, once the function finishes, the stack frame will be popped so the array goes out of scope along with the first element that your returned pointer refers to.In myfunc a pointer to a string literal is created and returned. The string literal does not exist on the stack because string literals have a static storage duration. Thus the returned pointer continues to refer to the string even after the function has returned because the string continues to exist throughout the lifetime of the program.   ;  sorry .. but i cant undertstand clearly ..myfunc returns a string which is in a constant place in memory, ?? why ??The string literal does not exist on the stack because string literals have a static storage duration.  .. could not understand what u meant by this .. could u people  explain more clearly can someone explain more clearly  ??regardsy2jsave   ;  Both are undefined behavior. If they work, it's a coincidence.   ;Quote:Original post by AntheusBoth are undefined behavior. If they work, it's a coincidence.How do you figure? A string literal has static storage duration (see 2.13.4 paragraph 1 in the standard) so passing pointers to it around is perfectly valid. If you're referring to binding a non-const char pointer to a string literal, that's explicitly allowed by section 4.2 paragraph 2 of the standard. It's deprecated, but well-defined.   ;  String literals are treated as constants.  So they are assigned memory and the memory is valid as long as the application is running.  So the second function has valid memory all the time.  The fun2 has memory assigned locally and will go out of scope when the function ends.  theTroll   ;Quote:Original post by y2jsavemyfunc returns a string which is in a constant place in memory, ?? why ??Generally you don't need to worry about such details. The reason, though, is that the string requires memory in order to exist but you really just want to be able to treat a string literal like any other literal, so:const char * get_str() { return "my string literal"; }int get_int() { return 5; }double get_double() { return 99.9; }All those literals are returned through functions. 5 is of type int, 99.9 is of type double and "my string literal" is of type const char*.Quote:The string literal does not exist on the stack because string literals have a static storage duration.  .. could not understand what u meant by this"The string literal (a hard-coded string) does not exist on the stack (the call stack where local variables for functions are created) because string literals have a static storage duration (they're stored in memory that remains the same throughout the entire lifetime of the program).";  thanks to u all ..got it now :)regardsy2jsave   ;Quote:Original post by dmatter"my string literal" is of type const char*.Common misconception. It's actually of type const char[18].   