+ 17
Does int *p=&x; is same as these two statements(int *p; p=&x; ) ?
7 Respostas
+ 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
+ 15
Ketan Lalcheta thanks now i understood ☺😊
+ 14
then
int *p;
*p=&x;
is wrong or right ? (x is previously declared)
+ 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
+ 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...
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 . ..
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...