xxxxxxxxxx
/*Difference between * and & in c++
you have to execute this code in your computer to undrestand otherwise this is not making sense
you can use online compilers such as
https://www.programiz.com/cpp-programming/online-compiler/
or
https://www.onlinegdb.com/online_c++_compiler
*/
#include <iostream>
using namespace std;
int main() {
int val = 5;
int& rref = val;
int* ptr = &val;
cout<<"in main" << endl
<< "val = " << val << endl
<< "&val = " << &val << endl
<< "rref = " << rref << endl
<< "&rref = " << &rref << endl
<< "ptr = " << ptr << endl
<< "*ptr = " << *ptr << endl
<< "&ptr = " << &ptr << endl;
/*
in main
val = 5
&val = 0x7ffd32ad1354
rref = 5
&rref = 0x7ffd32ad1354
ptr = 0x7ffd32ad1354
*ptr = 5
&ptr = 0x7ffd32ad1348
in the aspect of the appearance of code and understanding the code via the way
it looks like
this example shows that in c++ mentioning the name of rref in a function such
as cout<< is equivalent to the original value (val==rref)
and mentioning &rref is equivalent to address of original
((&val) or (&val==&rref))
while
mentioning the name of ptr is equivalent to address of original
((&val) or (&val==ptr))
and mentioning *ptr is equivalent to the original value (val==*ptr)
however, there is one more : that is &ptr that is not equivalent to any of those
in the above.
by some thinking even without reading from books and written we can come this
point that : probably that is because pointer variable itself has an extra
allocation compare to reference therefore in another language pointer is a
Reference to reference (somehow, even if not exactly)
terminologies might not be enough accurate and precise … please try to
rethink it.
*/
return 0;
}
xxxxxxxxxx
// The difference is here:
int a = 0;
int b = a++; // b = 0, a = 1
// Where
int a = 0;
int b = ++a; // b = 1, a = 1