Remix.run Logo
account42 4 days ago

> you can and should use char[] in both languages

Not for temporaries initialized from a string constant. That would create a new array on the stack which is rarely what you want.

And for globals this would preclude the the data backing your string from being shared with other instances of the same string (suffix) unless you use non-standard compiler options, which is again undesirable.

In modern C++ you probably want to convert to a string_view asap (ideally using the sv literal suffix) but that has problems with C interoperability.

TuxSH 4 days ago | parent [-]

Right, I've checked that char foo = "bar"; is indeed the same as the const char variant (both reference a string literal in rodata), which IMO makes it worse.

About string literals, the C23 standard states:

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
therefore `char foo = "bar";` is very bad practice (compared to using const char).

I assumed you wanted a mutable array of char initializable from a string literal, which is provided by std::string and char[] (depending on usecase).

> In modern C++ you probably want to convert to a string_view asap (ideally using the sv literal suffix)

I know as much