Home > front end >  function returns address of local variable [-Wreturn-local-addr] sprintf
function returns address of local variable [-Wreturn-local-addr] sprintf

Time:10-27

i ma new c and i am trying sprintf along with pointers. all i get in console is return buf; as is please help me with this code.

#include <stdio.h>
char* stringa(char* str);
int main()
{
    char* ss = "123";
    stringa(ss);

    return 0;
}

char* stringa( char* str)
{
    char buf [100] ;
    sprintf(buf,"hello %s", str);
    return buf;

}   

i tried many other ways too like sprintf_c and my computer shut down for serious. i am learning c.

CodePudding user response:

Maybe this is what you want

#include <stdio.h>
char* stringa(char* dest, char* src)

int main()
{
    char buf [100] ;
    char* ss = "123";
    printf("%s\n", stringa(buf, ss));

    return 0;
}

char* stringa(char* dest, char* src)
{
    sprintf(dest,"hello %s", src);
    return dest;
}

CodePudding user response:

In function 'char* stringa(char* str)' you are not allocating space in the heep for the char array 'buf' you are allocating space on the stack for that variable. (meaning after the function finishes, the variable 'buf' will be wiped away because it will be out of scope) therefore you must ask the compiler to allocate space in memory for this array, I recommend using malloc()

ex:

char* stringa( char* str)
{
char *buf  = (char*)malloc(sizeof(char) * 100);
sprintf(buf,"hello %s", str);
return buf;
}

CodePudding user response:

char* stringa( char* str)
{
    char buf [100] ;
    sprintf(buf,"hello %s", str);
    return buf;

}

The problem with this code is that the buf char array is local to the stringa function. When the function returns, the memory occupied by the buf array is not valid anymore (for example, it could be reused later to store the content of other variables, arrays, etc.).

So when the function returns, you are giving the caller a pointer to garbage memory, to invalid data. The C compiler is trying to help you with that warning message; it's telling you: "Sorry, you are trying to pass back to the caller the address of a local variable (i.e. the buf char array) that is not valid anymore when the function terminates."

To fix this problem one option could be to allocate the char array for the output string at the call site, and let the invoked stringa function write into the caller-provided array:

#include <stdio.h>

char* stringa(char* dest, const char* str);

int main()
{
    const char* ss = "123";
    char buf[100];
    stringa(buf, ss);

    return 0;
}

/* Write the final message into 'dest'.
 * Return the same dest address.
 */
char* stringa(char* dest, const char* str)
{
    /* Note: better using a safe string function 
     * to prevent buffer overflows (e.g. sprintf_s),
     * passing the maximum destination array size as well.
     */
    sprintf(dest,"hello %s", str);
    return dest;    
}   

Note that I also added some consts in your code to enforce some const-correctness for read-only input strings.

  • Related