const char pointer

When i use this code and print out cout << *szString; it only prints the first letter but when i print cout << szString; the whole word prints i thought char means one character and not a whole word, and this cout << szString[2] seems to work as well when i haven't used brackets in my declaration?. Can someone explain this please.

const char* szString = "Hello";

Last edited on
Using cout<< on a pointer usually just outputs the memory address but char* is very often used for strings so for that reason it has been overloaded to print the whole string. If this wasn't the case even things like cout << "Hello"; would not work.
What does it mean when char* is overloaded?
Overloaded functions are a number of functions (or operators) with the same name but with different parameter types.

The operator<< that is normally used on pointers is operator<<(const void*) (because any pointer can be implicit converted into a void*) but there is also an operator<<(const char*) that will be used instead when passing a char*.

operator<<(const void*) prints the memory address that the pointer points to.

operator<<(const char*) assumes the pointer points to the first character in a string and therefore it prints all characters in the string until it finds the null termination character '\0' that marks the end of the string.
Last edited on
Topic archived. No new replies allowed.