0

As I know, & is called 'reference operator' which means 'address of'. So, its role is storing address in any variable. For example, 'a=&b;'. But I know another meaning which is 'references'. As you know, references is a alias of a variable. So, in my result, & has two meaning according to position. If 'a=&b;', & means 'address of b'. If 'int &a = b;', & means 'alias of another variable'.

As I know, * is called 'dereference operator'. But It is like &, It has two meaning according to position. If 'int *a = &b', * means 'pointer variable'. If 'a=*b', * means 'dereference variable'.

Are they right????

P.S. I'm foriegner. So I'm poor at English. Sorry... my poor English.

Junu
  • 1
  • 2

1 Answers1

0

Hi as I understand you are having confusion with the concepts of pointers and references. Let me try and break it down for you :

When we use either of these in a variable declaration, think of it as specifying the datatype of that variable.

For example, int *a; creates a pointer variable 'a', which can hold the address of(in other words, point to) another integer variable.

Similarly int & a = b; creates a reference variable 'a' which refers to b(in other words 'a' is simply an alias to integer b).

Now it might look as they are same, infact they both serve similar functionality, but here are some differences:

A pointer has memory allocated for it to hold the address of another variable to which it points, whereas references don't actually store the address of the variable they are referencing.

Another difference would be that a pointer need not be initialized when it is declared, but a reference must be initialized when it is declared, otherwise it throws an error. ie; int * a; is ok whereas int & a; throws error.

Now for the operators, try and see them not at all associated with pointers or references(Although thats where we use them the most).

Reference operator(&) simply returns you the address of its operand variable. Whereas a dereferencing operator(*) simply assumes that the value of its argument is an address, and returns the value stored at that address.

Hope this helped you. Here are some useful references(no pun intended):

https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html

How does a C++ reference look, memory-wise?