Pointers and references

I was trying to understand this example for my programming course and what I don't get is why a and b don't have the same address. Is it possible that a pointer can point to two different addresses at the same time?

#include <iostream>

int main ()

{

int a = 5;
int b = 8;

const int* ptr =&a;
ptr = &b;




std::cout << ptr << a<< b
;
return 0;
}
why a and b don't have the same address


An object's address is where it exists in memory. let's take a look at a and b.

1
2
int a = 5;
int b = 8;


So a is one int object, and b is a different int object. They're two different objects. They're not the same object. They're two different objects. Each of them will exist at a different place in memory. So a will have a memory address, and because b is not the same object as a, b will have a different memory address.

That's why a and b don't have the same address. Because they're two different objects that exist in two different places in memory.
This is clear but why does the pointer have the same adress a a while its referencing is to b?
The pointer does not have the same address as a. For starters, you don't know the address of the pointer because nowhere in your code do you display it.

Even so, I know that the pointer does not have the same address as a, because the pointer is one object and a is a different object. Two different objects will have two different addresses.

Let's go through the code line by line and see what's happening.

int a = 5;
Create an int object, named a. Set the value of this object to 5. This object will be in some memory somewhere. For example, it might be in memory location 0x12341234. If we looked in that memory location, we would see the number 5.

int b = 8;
Create an int object, named b. Set the value of this object to 8. This object will be in some memory somewhere. For example, it might be in memory location 0x43214321. If we looked in that memory location, we would see the number 8.

const int* ptr =&a;
Create an int-pointer object, named ptr. This int-pointer will have an address; it exists somewhere in memory. Also, set the value of this int-pointer to the memory address of a. This does not change the memory address of the int-pointer; it changes the value of the int-pointer. If you looked at the int-pointer, wherever it is in memory, you would see the number 0x12341234 (using our example location of a from above)

ptr = &b;
Now change the value of the int-pointer. Set its value to the memory address of b. Using our example locations, ptr now has the value 0x43214321. The int-pointer itself is in the same place in memory; it just now stores a different value.

So the int-pointer was storing one memory address (the address of a), and then we changed what the int-pointer was storing, and made it store the memory address of b.

why does the pointer have the same adress a a while its referencing is to b?
It doesn't. Why do you think it does?
Last edited on
Problem solved... I found out that int* const is for a constant adress while const int* stands for constant values... thank you very much!!! :)
Topic archived. No new replies allowed.