+ 2
can anybody explain the difference between * and &
pointers
5 Respuestas
+ 9
You can read:
* as 'point-to'
& as 'address-of'
E.g.
int x = 5;
int* p;
p = &x;
cout << *p << endl;
cout << *&x << endl;
cout << &x << endl;
// the above can be read as:
/*
Declare x as integer variable which stores 5.
Declare pointer p of type integer.
Store the address of x into pointer p.
Point to address stored in p, and print the value.
Point to the address of x, and print the value.
Print the address of x.
+ 7
@yuri They are all the same.
0
* is used to declare a pointer variable like
int *point =....... ;
now & is ampersand operator u can use it to get the memory location like-
int *point = &var;
note: * is also used as a dereference operator this means that it is used to get value which is stored in that memory location like
*point;
0
just for clarification. are these the same:
1) int* p = &x;
2) int *p;
p = &x;
and another question.
where the space is to be :
int* p; or int *p; are they the same?
0
" * " denotes that the variable is pointer that stores the address of another variable while " & " is used to access the address of variable.
for example :
int a:
int *ptr;
*ptr = &a;
here *ptr is holding the address of integer a and &is used to access its address.