Is it safe to return a const char* assigned a string literal from a function?


kelemat

I know it's safe...

const char *get_str_literal() {
    return "I literally can't even";
}

But is it?

const char *get_str_literal() {
    const char *str = "I literally can't even";
    return str;
}

If not, why?

EDIT : How does the following differ from the second snippet above?

const char *get_str_literal() {
    const char str[] = "I literally can't even";
    return str;
}

Are the contents of string literals copied into automatic array storage? What's wrong?

nothing

In the second example:

const char *get_str_literal() {
    const char *str = "I literally can't even";
    return str;
}

stris a pointer set to a string literal with static storage duration. So when execution resumes in the calling function, the returned pointer will still point to something, a string literal.

In the last example:

const char *get_str_literal() {
    const char str[] = "I literally can't even";
    return str;
}

Character arrays str[]are initialized with string literals. After initialization, str[]is an array charof characters containing the string literal, up to and including the '\0'terminator. When strthe declaration is encountered return, it is converted into a pointer const char, which is returned to the calling function. However, str[]with automatic storage duration, its lifetime will end and the pointer will become indeterminate, resulting in indeterminate behavior.

Related


Return const char* from string literal in C++?

Bernard Typically, I would return a std::stringfrom a function , because returning a const char*would require the caller to provide an output memory buffer, which is not resizable. But const char*does it work if it is returned from a string literal ? const cha

Return const char* from string literal in C++?

Bernard Typically, I would return a std::stringfrom a function because returning a const char*would require the caller to provide an output memory buffer, which is not resizable. But const char*does it work if it is returned from a string literal ? const char*

Return const char* from string literal in C++?

Bernard Typically, I would return a std::stringfrom a function because returning a const char*would require the caller to provide an output memory buffer, which is not resizable. But const char*does it work if it is returned from a string literal ? const char*

String literal vs const char* function overloading

User 4054130 I have a function I want to use, const char*but it only works with string literals because they are given special rules to allow array initialization. The second overload is preferred foo(const char*)for both string literals and const char*s , but

String literal vs const char* function overloading

User 4054130 I have a function I want to use, const char*but it only works with string literals because they are given special rules to allow array initialization. The second overload is preferred foo(const char*)for both string literals and const char*s , but

Return a string literal from a function

Daniel I need a function to return a string based on a code value and a helper value, I think the sample code shows what I'm trying to do, and there might be an easy way to do this, but I'm stuck. There's going to be a lot of code numbers and this is the best

return const char* from custom printf function

Black Temple I have written a custom print function. My problem is that I need to return a const char*because it has to be used in another function . I don't know how to manage it at all... anotherFunction (const char* text /*Here*/, unsigned __int32 value, un

How to return string literal from Postgres function?

David Galbraith I got this working to return an integer: CREATE FUNCTION my_function() RETURNS INTEGER AS $$ SELECT 1 AS result; $$ LANGUAGE SQL; But I wanted it to return a string, so I adjusted it to: CREATE FUNCTION my_function() RETURNS TEXT AS $$ SEL

Return a string literal from one function to another

Manas Swain I'm really struggling to figure out how to return strings from one function to another. Please help me to solve this problem. Return the password as a string from this function: char* password(void) { const maxPassword = 15; char password[maxPa