Pointer Variable

Silly question but interesting....
I know the answers but dont know why so ???

I have an integer pointer and i want its address without allocating memory,

main()
{
int *a;
cout<<a;
}
this is giving 00000000 and its but obvious.

now if i use address of a (&a) along with *a,

main()
{
int *a;
cout<<a;
cout<<&a;
}

'cout<<a' gives me a constant address but 'cout<<&a' gives me different address.

what is the reason behind & and why behaviour of 'cout<<a' changes when using with &.
It is not obvious that an uninitialised builtin type is initially assigned zero. Nor is it always true. It just happens to be zero when you ran the app. Try compiling for release and see what happens.

a is a variable like any other. It has an address so you can do &a. As it's a pointer, you can do *a. The results are not deterministic in any event. This is because *a is an uninitialised value and &a is determined by the runtime environment.
@Bhavesh205
this is giving 00000000 and its but obvious.


You are mistaken. pointer a can have any value because it was not initialized.

Here

cout<<a;

the value stored in a (that is an undefined value) is displayed.

And here

cout<<&a;

the address of the variable itself is displayed.
agree with both of you but as soon as i use cout<<&a, 'cout<<a' giving me a constant value, what is the reason behind this ?
1
2
3
4
5
6
7
8
9
10
int x = 5
int *y = &x;
int *z = &x;

std::cout <<  x << std::endl;
std::cout << &x << std::endl;
std::cout <<  y << std::endl;
std::cout << &y << std::endl;
std::cout <<  z << std::endl;
std::cout << &z << std::endl;
The second, third, and fifth values should be the same, the rest should be different.
Last edited on
@kbw : you have put a good point, i try it on release and find that 'cout<<a' is not changing while using 'cout<<&a'.
The value is not guaranteed to be 0, it is uninitialized. It is 0 by luck.
@Bhavesh205
i try it on release and find that 'cout<<a' is not changing while using 'cout<<&a'.


Could you explain why do you think that a shall be changed after the statement 'cout<<&a;'?
It seems that you understood nothing what you were sayed avout. Please reread our previous messages.
Each variable (so also any pointer type) has an address and a value. The address can be retrieved by putting '&' before it's name, the value by the name of the variable itself:
1
2
3
4
5
6
7
8
9
10
11
12
int a = 5;
std::cout << a;  // prints the value in a, which is 5
std::cout << &a; // prints the memory address of a, which differs per run of the program

int* p;
std::cout << p;  // prints the value in p: some garbage, differs per run
std::cout << &p; // prints the memory address of p (remember, a pointer is just a normal variable, and therefore it has a place in memory)

p = &a;           // store the address of a in p
std::cout << p;  // prints the value of p, which is now the address of a
std::cout << &p; // logically prints the address of p again
std::cout << *p; // the '*' operator can only be used with pointers: here it prints the value that is on the memory address that p stores: that memory address is the address of a, so it prints the value of a: 5 
Last edited on
Topic archived. No new replies allowed.