#include<iostream>
using namespace std;

int main(){
int fish =6;
int * fishp = &fish;

cout<<fishp<<endl;

return 0;}

this runs ok, but i get the same memory address when i cout either fishp or &fishp. how is it possible for a pointer to
have the same address as the address it's pointing to?

Recommended Answers

All 6 Replies

Maybe you did something wrong. This is what I get

00AFFF10 00AFFF04
Press any key to continue . . .

#include<iostream>
using namespace std;

int main(){
    int fish = 6;
    int * fishp = &fish;

    cout << fishp << ' ' << &fishp << endl;

    return 0;
}

Are you trying to do this? cout << fishp << ' ' << &fish << endl;
Then yes, they will both be the same because fishp points to &fish.

no please, that wasn't the intention.. i wanted the memory address of the variable fish, and that of the pointer fishp. but thanx a lot :)

also, i didn't print both out at the same time.. i run it the first time to cout fishp, then changed the program and run it again to cout &fishp and they all gave same memory address; but if i print both out at the same time, i get different 0x figures

What compiler are you using? I tried that and got two different addresses.

but i get the same memory address when i cout either fishp or &fishp.

Not possible.

if you use cout<<fishp then it print the address of the variable , it points to(therefore print the address of fish).

when you use cout<<&fishp then it print the address of the variable(fishp) itself.

therefore two differents addresses will be printed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.