We help IT Professionals succeed at work.

Under what situations do we use & and *

YvonP
YvonP asked
on
I know that they are used for some implied meanings like array or  object reference.
Safety purpose, not possible to create reference easily without instantiation.[also let me know to create reference with out instantiation]

Any other reason???
Comment
Watch Question

Top Expert 2009
Commented:
Your question is not very clear, and it's posted in the C zone, even though it seems you're talking about C++.

Are you asking when to use references versus pointers in C++ ?

If so, the answer is simple : whenever you can, use references, otherwise, use pointers.

References are in many respects safer than pointers, which is why they are preferred. In some cases, you will not be able to use references though (like in containers, or when NULL is a valid value, or ...), so then you'll have to revert to pointers.
Commented:
classical case:
int x;
int* p = &x;
int& r = x;
 'p' and 'r' allow indirect access to the same variable. 'p' is the pointer, 'r' is the reference.
'r' is a second name for 'x'. Whatever you do with 'r', you will see in 'x'.
Pointer gives you more "freedom" (with all worries related to it). You can allocate a memory block, for example, for few integer values and 'p' from the example above allows to work with all of them - you can use such operations as p++, p += 2, etc. Pointer can point to many different objects. Reference can refer only one.
Read this articles:
References vs. Pointers
http://www.embedded.com/story/OEG20010311S0024
 http://www.cplusplus.com/forum/beginner/3958/
According to your Q. title "Under what situations do we  use & and * "

If u need to know what is address of a variable then use &
&--------------------------> Address of operator.
*---------------------------> Indirection operator also known as value at optr.

If u want to get the value at a specific address then use *.

But in C++, & having one more meaning i.e reference. which is already answered above by Infinity and pqnatyuk.