Difference between * and & ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Difference between * and & ...

I can't understand

20th Feb 2017, 7:25 AM
Arun
Arun - avatar
2 Answers
+ 8
& returns the memory address of a variable, or where the variable is being stored. * is used to declare pointers, and returns the value at a memory address. Or more, it goes to where you say something is stored and tells you what it is. Ex: "int* p = #" stores num's address inside *p. So using *p is just like using num. Using p alone or &num gives a hexadecimal memory address.
20th Feb 2017, 7:43 AM
Tamra
Tamra - avatar
+ 1
It depends on where are you using them. & Operator: Example 1: 'int &r = x;' or, 'int& r = x;' Here & is used to denote that r is a reference variable. So, here & is a reference operator. To know more about C++ reference variable: http://www.cprogramming.com/tutorial/references.html Example 2: 'int *p = &x;' Here & is "address of" operator. That means you are assigning the address of x in pointer p. * Operator: Example 1: 'int *p = &x;' Here * is used to denote that p is a pointer variable which points to an int . Example 2: 'cout << *p << "\n";' Here * is a dereference operator. Dereference is used, to access the content of the object the pointer is pointing. N.B: In C++ same operator has many forms. This is called operator overloading. You will learn it in advance level.
21st Feb 2017, 8:21 PM
Deleted