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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More