C++ Pointers Concept with Example

In this tutorials we will study and understand the concept of Pointers in C++.

 Pointers are powerful features of C++ that enables you to manipulate the data in the computer’s memory directly.
To understand pointers, you should first know how data is stored on the computer.
Every variable in a C++ program is stored in the computer memory and this memory has a unique memory address.
To know where the data is stored, C++ has an & operator. The & (reference) operator gives you the address occupied by a variable.
If var is a variable then, &var gives the address of that variable.

The 0x in the beginning represents the address is in hexadecimal form.
pointers in c++
Pointers in c++ are variables that points to a specific address in the memory occupied by another variable. A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is:

Here the type is the type of variable the pointer will point to. The * symbol denotes that this variable is a pointer variable.
Following are the valid pointer declaration:

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address which is of 2-bytes. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

Reference operator (&) and Deference operator (*)

Reference operator (&) as discussed above gives the address of a variable.
To get the value stored in the memory address, we use the dereference operator (*).
For example: If a number variable is stored in the memory address 0x123, and it contains a value 5.
The reference (&) operator gives the value 0x123, while the dereference (*) operator gives the value 5.

Example Program of Pointers in C++

Run Online

Pointers can be used in different ways to perform different types of task. Some uses, variations and topics of pointer are as follows:

  • Pointer to Arrays
  • Pointer to Pointer
  • Pointer to Functions
  • Pointer with structures
  • Dynamic Memory Allocation
  • Null Pointer
Watch it on YouTube

3 thoughts on “C++ Pointers Concept with Example

Leave a Reply

Your email address will not be published. Required fields are marked *