c++

C++ Pointers

In this article, we are going to learn about pointers , its syntax and some special points about it.

Storing addresses in C++

As from before discussion we know that every variable in C++ when declared have a unique address in the memory. What if we want to store these address. It is not possible for a normal variable to store the address of other variable so here the concept of pointer comes.


What is a pointer?

Pointers are those special variables which are meant for storing addresses of other variables.

General syntax for declaring a pointer

<data_type>* <pointer_name>
How do you decide the data type of pointer?

Pointers data type is decided by seeing the data type of variable whose address is to be stored in that pointer i.e. pointers data type should be same as the data type of the variable it is pointing to.

Here is how we can declare pointers.

int *p;

Here, we have declared a pointer p of the type int.

We can also declare pointers in the following way.

int* p; // preferred syntax

Note : The * operator is used after the data type to declare pointers.

Assigning Addresses to Pointers

Here is how we can assign addresses to pointers:

int* p ,a=10; 

// assign address of var to pointVar pointer 
p = &a;

Here, 10 is assigned to the variable a. And, the address of a is assigned to the p pointer with the code p = &a. In other words, pointer p of integer type is pointing to the memory address of a.

Get the Value from the Address Using Pointers

To get the value pointed by a pointer, we use the * operator this operator is also known as dereferencing operator. For example:

int* p, a=10; 

// assign address of a to p 
p = &a; 

// access value pointed by p 
cout << *p << endl; // Output: 10

In the above code, the address of a is assigned to the p pointer. We have used the *p to get the value stored in that address.

When * is used with pointers, it's called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *p = a.

Note : In C++, p and *p is completely different. We cannot do something like *p = &a;.


Working of C++ Pointers

#include <iostream>

using namespace std;

int main() {
    int a = 10;

    // declare pointer variable
    int* p;

    // store address of a
    p = &a;

    // print value of a
    cout << "a = " << a << endl;

    // print address of a
    cout << "Address of a (&a) = " << &a << endl;

    // print pointer p
    cout << "p = " << p << endl;

    // print the content of the address p points to
    cout << "Content of the address pointed to by p (*p) = " << *p << endl;

    return 0;
}

Output :

a = 10
Address of a (&a) = 0x61ff10
pointVar = 0x61ff10
Content of the address pointed to by p (*p) = 10
img

Changing Value Pointed by Pointers

#include <iostream>

using namespace std;

int main() {
    int a =10;
    int* p;

    // store address of a
    p = &a;

    // print a
    cout << "a = " << a << endl;

    // print *p
    cout << "*p = " << *p << endl ;

    cout << "Changing value of a to 17:" << endl;

    // change value of a to 17
    a = 17;

    // print a
    cout << "a = " << a << endl;

    // print *p
    cout << "*p = " << *p << endl;

    cout << "Changing value of *p to 19:" << endl;

    // change value of a to 19
    *p = 19;

    // print a
    cout << "a = " << a << endl;

    // print *p
    cout << "*p = " << *p << endl;

    return 0;
}

Output :

a = 10
*p = 10

Changing value of a to 17:
a = 17
*p = 17

Changing value of *p to 19:
a = 19
*p = 19

Some special points about pointers :
  1. The data types of the pointers is always kept same as the data types of variables to whom the pointer will point.
  2. Formally, we must read the declaration of a pointer from right to left. So the following line: int *p; should be read as “p is a pointer to an integer” and and not integer pointer p.
  3. The size of the pointer never depends on its data type. The size of pointer in turbo compiler is of 2 bytes and in GCC compiler it is of 4 bytes.
  4. Whenever we use the symbol of * with the pointer name declaration, we read it in a very special way. The expression: *p; is read as “value at address stored in p” or simply “value at p”.
Common mistakes when working with pointers

Suppose, we want a pointer p to point to the address of a. Then,

int a, *p; 

// Wrong! 
// p is an address but a is not 
p = a; 

// Wrong! 
// &a is an address
 // *p is the value stored in &a 
 *p = &a; 
 
 // Correct! 
 // p is an address and so is &a 
 p = &a; 
 
 // Correct! 
 // both *p and a are values 
 *p = a;