Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than actual of data.

Pointer Declaration



Here data-type is one of the C++ data types, pointer-variable is the name of pointer and * (asterisk) refers to the pointer. Asterisk (*) can be placed at three different locations as:

  1. Place immediately before pointer-variable.
  2. Place between the data type and the pointer variable.
  3. Place immediately after the data type.



Pointer Initialization


Here ptr is a pointer variable, x is variable of int data type. &x contains the address of variable x and prt = &x; assign address of x into ptr.


Deference operator (*) and Reference operator (&)

Deference operator (*) gives the value of variable stored at the memory location. Reference operator (&) gives the memory address of a variable.

For example: If a number variable is stored in the memory address 0x22, and it contains a value 10.

Dereference (*) operator gives the value 10, while reference (&) operator gives the value 0x22.

C++ Pointer Example


Pin It on Pinterest

Share This