So I did the following test:
char* a = "test";
char* b = "test";
char* c = "test\0";
And now the questions:
1) Is it guaranteed that a==b? I know I’m comparing addresses. This is not meant to compare the strings, but whether identical string literals are stored in a single memory location
2) Why doesn’t a==c? Shouldn’t the compiler be able to see that they’re referring to the same string?
3) Is an extra \0 appended at the end of c, even though it already contains one?
I didn’t want to ask 3 different questions for this because they seem somehow related, sorry ’bout that.
Note: The tag is correct, I’m interested in C++. (although please specify if the behavior is different for C)
No. But it is allowed by §2.14.5/12:
And as you can see from that last sentence using
char*instead ofchar const*is a recipe for trouble (and your compiler should be rejecting it; make sure you have warnings enabled and high conformance levels selected).No, they’re not required to be referring to same array of characters. One has five elements, the other six. An implementation could store the two in overlapping storage, but that’s not required.
Yes.