The Wayback Machine - https://web.archive.org/web/20240204103257/https://www.geeksforgeeks.org/cpp-pointer-operators/
Open In App
Related Articles

C++ Pointer Operators

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Pointers in C++

A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it.

Syntax:

int *pointer_name; 

There are mainly two types of Pointer operators mainly used:

  • Address of operator (&)
  • The indirection operator/Dereference operator (*)
Image showing the relation between pointer and variable

Image showing the relation between pointer and variable

1. Address-of operator (&)

The Address-of operator (&) is a unary operator that returns the memory address of its operand which means it stores the address of the variable, which depicts that we are only storing the address not the numerical value of the operand. It is spelled as the address of the variable.

Syntax: 

gfg = &x; // the variable gfg stores the address of the variable x.

Example:

C++




// C++ Program to demonstrate
// the use of Address-of operator (&)
#include <iostream>
using namespace std;
 
int main()
{
 
    int x = 20;
 
    // Pointer pointing towards x
    int* ptr = &x;
 
    cout << "The address of the variable x is :- " << ptr;
    return 0;
}


Output

The address of the variable x is :- 0x7fff412f512c

2. The indirection operator/Dereference operator (*)

The indirection/ dereference operator is a unary operator that returns the value of the variable present at the given address. It is completely opposite to the address-of operator. It is spelled as a value pointed at the address.

Example:

C++




// C++ Program  to Demonstrate
// the indirection/Dereference operator
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    int self_paced = 3899;
    int* price;
 
    // Pointer storing address of self_paced
    price = &self;_paced;
 
    cout
        << "The value stored at the variable price is Rs : "
        << (*price);
 
    return 0;
}


Output

The value stored at the variable price is Rs : 3899

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 20 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials