I have learned that a pointer points to a memory address so i can use it to alter the value set at that address. So like this:
int *pPointer = &iTuna;
pPointer here has the memory address of iTuna. So we can use pPointer to alter the value at iTuna. If I print pPointer the memory address gets printed and if I print *pPointer then the value at iTuna gets printed
Now see this program
char* pStr= "Hello !";
cout<< pStr << endl;
cout<< *pStr << endl;
system("PAUSE");
return 0;
There are a lot of stuff I don’t understand here:
-
In “Hello !” Each letter is stored separately, and a pointer holds one memory address. So how does
pStrpoint to all the letters. -
Also When I print out
pStrit prints Hello !, not a memory address. -
And when I print out
*pStrit prints out H only not all whatpStris pointing too.
I really can’t understand and these are my concerns. I hope someone can explain to me how this works ad help me understand
"Hello !"is an array of typechar const[8]and value{ 'H', 'e', 'l', 'l', 'o', ' ', '!', 0 }.pStris a pointer to its first element; its last element has the value0.There is an overload in the iostreams library for a
char const *argument, which treats the argument as a pointer to the first element of an array and prints every element until it encounters a zero. (“Null-terminated string” in colloquial parlance.)Dereferencing the pointer to the first element of an array gives you the first element of the array, i.e.
'H'. This identical topStr[0].