can anyone tell in detail following example. int a=10; int *p; *p=&a; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can anyone tell in detail following example. int a=10; int *p; *p=&a;

13th Sep 2016, 8:15 AM
susmitha gummadi
susmitha gummadi - avatar
2 Answers
+ 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
13th Sep 2016, 8:27 AM
Zen
Zen - avatar
+ 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.
13th Sep 2016, 2:26 PM
subodh Kumar
subodh Kumar - avatar