compiler is giving the waring |
What warning is that?
Does it say:
This here char* wordtwo = "simplebee"; takes address of memory that THOU SHALT NOT MODIFY, but you dare to save the address in non-const pointer! |
I hope it does.
These two have quite different initializations:
1 2
|
const char* foo = "hello";
char bar[] = "Dolly";
| |
The first one creates a pointer and
stores the address of "hello" into it. The "hello" exists elsewhere in memory.
The second allocates 6 bytes of memory for an array and then
copies characters of "Dolly" into that array.
Printing is a different situation:
1 2
|
std::cout << foo;
std::cout << bar;
| |
Operator
<<
is a function. There are many overloads of that function.
Guess which is used when you "print pointer"? The
(std::ostream&, const char*)
http://www.cplusplus.com/reference/ostream/ostream/operator-free/
Now guess, which is used to "print array"?
I can tell that it is not a version that would take an array as argument, because no function takes a plain array as argument.
The correct answer is:
(std::ostream&, const char*)
Yes, the array('s address) is converted into
const char*
pointer and that pointer goes to the function. (There are actually two conversions: (1) from char array into char* and (2) from char* into const char*.)
In other words, the same "print a C-string" operator is used for char pointers and char arrays.