0
can anyone tell in detail following example. int a=10; int *p; *p=&a;
2 ответов
+ 3
Hum, this code is wrong. It should be either:
int a = 10;
int *p = &a;
or
int a = 10;
int *p;
p = &a;
In both cases, you declare an int a and assign 10 to it, then you declare an int pointer p and assign it the address of a.
You can access the value of a with the pointer by dereferencing it with *:
cout << *p; //prints 10
And of course, since it's a pointer to a, changing the value of a will also change the value of *p (the value is litterally the same, they look at the same memory space).
a = 5;
cout << *p; //prints 5
+ 2
above code will give compilation error as we r trying to store address instead of integer value.
in above code p is a pointer who can store the address of int variable but we r putting address at address stored (here p is pointing random address) in p.