can anybody explain the difference between * and & | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

can anybody explain the difference between * and &

pointers

3rd Aug 2017, 8:55 AM
Eshaan Belani
5 Answers
+ 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.
3rd Aug 2017, 9:11 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
@yuri They are all the same.
3rd Aug 2017, 11:54 AM
Hatsy Rei
Hatsy Rei - avatar
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;
3rd Aug 2017, 9:00 AM
shobhit
shobhit - avatar
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?
3rd Aug 2017, 9:54 AM
yuri
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.
3rd Aug 2017, 4:23 PM
Nilesh Halge
Nilesh Halge - avatar