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

Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More