+ 2
Difference between * and & ...
I can't understand
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.
+ 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.