char *name = "abcde";
Assigning a string literal (aka a const char[]
) to a non-const char*
pointer is deprecated in C++11, thus the warning.
This declaration sets name
to point at the starting address of the string literal's character data.
String literals reside in read-only memory. Code will crash if it tries to write to read-only memory using a non-const pointer. Pointers to literal data should be declared as const
, eg: const char *name = "abcde";
char name[] = "abcde";
Initializing a char[]
buffer (const or otherwise) with a string literal is allowed, so no warning.
This declaration allocates name
at runtime to the full length of the string literal and then copies the string literal's character data into it.
This explains the difference in address outputs. One is an address in read-only memory, the other is an address in stack memory.