Dec 7, 2015 at 3:13am UTC
I'm still getting used to pointers. It seems like the more I study them the more confused I get : )
Anyway, code below:
1 2
for (int *pnPtr = myStringArray; pnPtr < myStringArray + arraySize; pnPtr += 1)
{cout << "pnPtr: " << pnPtr <<"\t&pnPtr: " <<&pnPtr<<"\t*pnPtr: " << *pnPtr << endl;}
address of pointer pnPtr-->&pnPtr
dereferencing pnPtr---->*pnPtr
?? ---->cout<<pnPtr
I get a hexadecimal number here. What is pnPtr?
pnPtr: 0x23fdd0 &pnPtr: 0x23fdc8 *pnPtr: 1
pnPtr: 0x23fdd4 &pnPtr: 0x23fdc8 *pnPtr: 2
pnPtr: 0x23fdd8 &pnPtr: 0x23fdc8 *pnPtr: 3
pnPtr: 0x23fddc &pnPtr: 0x23fdc8 *pnPtr: 4
pnPtr: 0x23fde0 &pnPtr: 0x23fdc8 *pnPtr: 5
pnPtr: 0x23fde4 &pnPtr: 0x23fdc8 *pnPtr: 10
pnPtr: 0x23fde8 &pnPtr: 0x23fdc8 *pnPtr: 20
Last edited on Dec 7, 2015 at 3:14am UTC
Dec 7, 2015 at 3:31am UTC
cout << (pointer_to_almost_anything)
produces a hex number that represents the pointer value. The exception is cout << pointer_to_char which assumes the pointer is to a null-terminated string and prints the string.
Now, pnPtr is a pointer. And &pnPtr is also a pointer, so those two print the hex strings that you see.
Dec 7, 2015 at 4:29am UTC
cout << pnPtr prints the address of the value the pointer points to while cout << &pnPtr prints the address of the pointer itself.