Pointers and Arrays Question

nothingasis

In Runtime
Messages
198
Location
CA
Code::

int Array[] = {1,2,3,4,5,6};
int *Ptr = 0;
iPtr = &Array[0];

Ptr = Ptr - 2;
cout << *Ptr << endl; //Outputs the value -858993460

Hello again, here with another question :). I wasn't sure how to even ask this without displaying the code so searching kind of went no good for me. But can someone please explain?

Everything works properly but my question is when I put Ptr on watch the value = 0x0028fb54 which I understand is a memory address, and expanded it gives me a value of -858993460 but what is that value?
 
That is the value that the pointer is pointing to.

*Ptr dereferences the pointer, and shows the value it's pointing to. If you do:
Code:
cout << Ptr << endl;
it will print out the address of the pointer.

It's printing -858993460, because you're moving the memory address the pointer is pointing to, with this line:
Code:
Ptr = Ptr - 2;

Since you're not dereferencing the pointer when you perform the operation, you're actually moving the memory address rather than modifying the value. To modify the value the pointer is pointing to, you would want to do:
Code:
*Ptr = *Ptr - 2

It's been a while since I've done stuff with pointers (I've always hated pointers...blech...), but I believe that's what it's doing.
 
Back
Top Bottom