An example of a pointer is
int i=10;
int *ptr;
ptr=&i;
here, i was told that the "&" sign stands for "address of"... so the above line should be read as pointer to an integer i is the address of i....
however while writing references to variables we generally write
int i=10;
int &j=i;
doesn't this translate to address of j = 10?? and we know we cannot allocate address to a variable so how is the above line actually work?! because this line is supposed to be "j=i=10;"
Just like spoken languages, context can change the meaning of a word or symbol. '*' can represent a pointer type, dereference or multiplication. '&' can represent "address of", or a reference type. Both depend on the context of their use.
In the context of pointers, the ampersand is used to reveal the address of the right-hand operand of the ampersand. The pointer then takes the address and points to it, while maintaining its own address in memory. When null is assigned to a pointer, the pointer is considered not to be pointing to a valid address, hence the reason why access violations occur when dereferencing a null pointer.
Pointers can be used in two cases: When a parameter is optional and when DMA is required.
In the context of references, the ampersand is used to declare a reference to another object that is of the same type. With references, you don't have to explicitly specify the address of the target object as you do with pointers. References are declared constant implicitly (constant reference, not value), which means the reference cannot refer to another object other than the one it's currently referring to, but can modify the object it's referring to, unless explicitly declared constant. References must have an initializer and cannot be null.
References can be used when: Parameters are a must and when an object must always refer to an object.
thanks everyone for replying so quickly... !
when i asked the person who's teaching me... she said that when you use references, it is actually the concept of pointers that is going on behind the scenes and told me to figure out the rest by myself... thats my assignment now... and i cant figure out what exactly goes on behind d scenes...