How to return a string literal from a function
        Posted  
        
            by skydoor
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by skydoor
        
        
        
        Published on 2010-03-20T01:30:58Z
        Indexed on 
            2010/03/20
            1:41 UTC
        
        
        Read the original article
        Hit count: 292
        
c++
Hi I am always confused about return a string literal or a string from a function. I was told that there might be memory leak because you don't know when the memory will be deleted?
For example, in the code below, how to implement foo() so that make the output of the code is "Hello World"?
void foo (       )              // you can add parameters here.
{
}
int main ()
{
    char *c;
    foo (    );
    printf ("%s",c);
    return 0;
}
Also if the return type of foo() is not void, but you can return char*, what should it be.
© Stack Overflow or respective owner