+ 17

Does int *p=&x; is same as these two statements(int *p; p=&x; ) ?

12th Aug 2018, 8:33 PM
Srikant
Srikant - avatar
7 odpowiedzi
+ 3
SRIKANT SAHOO wrong... p is pointer which point to x...so, it holds address of x hence, we write p=&x.... int* is data type of p...which is required at time of declaration... now if you use *p after declaration, it means you are trying to access value pointed by x... to conclude, *p =&x is not correct EDIT : in single line declaration also, we use p = &x as below : int* p = &x; here, p is variable name which is of pointer type int.. int* is data type
13th Aug 2018, 6:59 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 15
Ketan Lalcheta thanks now i understood ☺😊
13th Aug 2018, 7:08 PM
Srikant
Srikant - avatar
+ 14
then int *p; *p=&x; is wrong or right ? (x is previously declared)
13th Aug 2018, 6:55 PM
Srikant
Srikant - avatar
+ 1
yes, the asterisk used just in declaration if you use it in in different place it's will access the content of that address
12th Aug 2018, 9:38 PM
Bareq H. AL-Taamah
Bareq H. AL-Taamah - avatar
+ 1
yes SRIKANT SAHOO .. consider int a = 5 ; and int a; a = 5; in first case, you are assigning value of variable a with declaration itself... in second case, first you have declared variable a and then you have assigned its value as 5.. same philosophy is applied to your scenario of pointer...
13th Aug 2018, 5:10 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
yep. *p mean u are declaring a pointer variable means u are creating a variable holding address of another variable. and &p means u r refering to the content of the pointer variable . ..
13th Aug 2018, 6:42 AM
zuhaib
0
just do it like below in code playground and verify results: int x = 5; int*p = &x; x = 7; cout << p << endl; cout << *p << endl; cout << x << endl; with above, output for second and third will be 7 (p is pointer and *p is value pointed by pointer).. output of first line will be memory address rather than value of x... try to run code with whatever you are asking & check for error...
13th Aug 2018, 7:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar