I am having a problem about function argument type. Please help me understand this.
Here is the definition of printOnNewLine():
1 2 3 4
void printOnNewLine(char*x)
{
cout << x << endl;
}
printOnNewLine("hello");
I am wondering why printOnNewLine("hello") work.
Is "hello" considered as a pointer?
I think it is not. "hello" is a string and there is a pointer that points to the first character of that string (the character 'h').
Let's assume that the pointer is pS with the address value, say 0x000FE.
Then what will be printed on the screen, 0x000FE or the string "hello".
"hello" is a pointer. In particular, it is a constchar*. It is a pointer to the first char in memory where the characters 'h' 'e' 'l' 'l' 'o' '\0' are stored.
I know the declaration below with str is a pointer to the first char in memory of "hello" but never thought that "hello" is also a pointer to the first char of the string!