C++ Pointers3 min read

Pointer is an important part of the programming as it deals with the memory address of the variables. There are various tasks that can be achieved easily with the use of pointers in C++ such as dynamic memory allocation that needs pointers for its operation. It also minimizes the execution time and saves memory space. Arrays, function and structure also use pointers to manipulate the variables and parameters.

Let us understand the memory address first.

Memory Address:

As we already know at this point that every variable declared in a program is stored in computer storage possessing some memory address. And these addresses can be accessed by using the ampersand (&) operator. If you have the variable name var then &var is used to access the address of that variable in a program.

Let us see ampersand (&) in action.

C++ program to print the address of variables using pointers.

Output:

The address on displayed on your computer may be different. It address is in hexadecimal representation.


C++ Pointer

In programming, pointers refer to the symbolic representation of an address. Pointers are the variables that store the address of another variable (the variable the pointer is pointing to) rather than the value itself.

Syntax of Pointer:

Declaration of pointer in C++: Pointer variables are declared in the following way using asterisk * symbol ( asterisk * = same asterisk used for multiplication).

Example: You can declare the pointer with different C++ data-types too, some are:

Initialization of pointers

After declaring the pointer, we need to initialize the pointers. To get the actual address we use the ampersand (&) operator and it is used before the variable name that pointer needed to be the point.

Example:


  • Reference operator (&): It returns the variable’s address.
  • Dereference operator (*): It gets the value that has been stored in a memory address.

How to use a Pointer?

  • First, we define a pointer.
  • Then, assigning an address of a variable to a pointer using a Reference operator (&).
  • Lastly, with the use of Dereference operator (*), we access the value present at the address pointed by the pointer variable.

Pointer Example

Let us see a C++ program to print the address and the value with Pointers.

Output:


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More