I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an
Points to keep in mind:
Pointers can be NULL
, references cannot be NULL
.
References are easier to use, const
can be used for a reference when we don't want to change value and just need a reference in a function.
Pointer used with a *
while references used with a &
.
Use pointers when pointer arithmetic operation are required.
You can have pointers to a void type int a=5; void *p = &a;
but cannot have a reference to a void type.
Pointer Vs Reference
void fun(int *a)
{
cout<
Verdict when to use what
Pointer: For array, linklist, tree implementations and pointer arithmetic.
Reference: In function parameters and return types.