What is difference between a pointer and reference parameter in C++?

C++Server Side ProgrammingProgramming

Pointers

Pointer variables are used to store the address of variable.

Syntax

Type *pointer;

Initialization

Type *pointer;
Pointer=variable name;

References

When a parameter is declared as reference, it becomes an alternative name for an existing parameter.

Syntax

Type &newname=existing name;

Initialization

Type &pointer;
Pointer=variable name;

The main differences between pointers and reference parameters are −

  • References are used to refer an existing variable in another name whereas pointers are used to store address of variable.

  • References cannot have a null value assigned but pointer can.

  • A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.

  • A reference must be initialized on declaration while it is not necessary in case of pointer.

  • A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack.

raja
Published on 18-Jun-2019 15:56:28
Advertisements