Do pointers to string literals remain valid after a function returns?

后端 未结 4 513
温柔的废话
温柔的废话 2020-11-30 09:48

Is the pointer returned by the following function valid?

const char * bool2str( bool flg )
{
    return flg ? \"Yes\" : \"No\";
}

It works

4条回答
  •  感动是毒
    2020-11-30 10:43

    This code is perfectly valid and conformant. The only "gotcha" would be to ensure that the caller doesn't try to free the string.

提交回复
热议问题