I've just started learning C recently and have this question about pointer to string.
Given char* colour = {"Blue"};
, why does cout << colour
return Blue? Isn't colour a pointer and should return an address instead?
Asked
Active
Viewed 31 times
1

Khanh
- 57
- 7
-
4First thing to learn is that the code you have shown is not C, it is C++. C and C++ are different languages. – kaylum Aug 14 '22 at 04:10
-
1`char*` is a pointer _to a single character_, but the way a string is stored in C is as a pointer to the character that starts it -- the user of the string is supposed to know to start there and keep going until the next NUL. The C++ standard library has a separate string representation, but I don't know why it would be surprising for the C++ standard library to provide an `operator<<` implementation that knows how to print a legacy C-style string. – Charles Duffy Aug 14 '22 at 04:14
-
1Thanks @kaylum and charles duffy. I've got my answer. – Khanh Aug 14 '22 at 04:40