I was wondering what the differences are between the following definitions:
// file.cpp:
namespace n
{
static char const * const str1 = "hello";
static char const str2[] = "hello";
}
Behaviors I want, and I think they both provide:
- They both refer to immutable data (because the data is char consts)
- Neither variable can be modified (because str1 is defined as a * const and because str2 is an array, which can't be used as an l-value?)
- They both have internal linkage (via static)
- They both have namespace scope
- If a pointer to either string data is made available to a different module (via some function not specified here), the memory for those strings will be valid (str1 because it points to a string literal, and str2 because the array is declared at namespace scope)
Is there any differences that are guaranteed by the language? If there are behaviors that are implementation dependent, how can I investigate the difference on my different platforms?
(For this example, I am not interested in contrasting these behaviors with std::string options, although feel free to talk about that too if you think other readers would be interested.)