Pointers In C++

Pointers In C++

Through the use of examples, we will learn about pointers in C++ and how they operate in this tutorial.

Addresses are symbolically represented by pointers. They give programs the ability to generate and manipulate dynamic data structures in addition to simulating call-by-reference.

Learning C++ pointers is simple and enjoyable. Pointers make some C++ operations easier to complete while preventing others, like dynamic memory allocation, from being completed.

Pointers are variables in C++ that keep track of other variables’ memory addresses.

Address in C++

If we have a variable named var in our program, the command &var will provide its memory address. For instance,

Every variable is a memory location, and each memory location has a determined address that may be accessed using the ampersand (&) operator, which stands for a memory address. Take into account the following, which will print the defined variables’ addresses:

Example 1: Printing Variable Addresses in C++

include

using namespace std;

int main()
{
// declare variables
int var1 = 3;
int var2 = 24;
int var3 = 17;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// print address of var1
cout << "Address of var1: "<< &var1 << endl;
// print address of var2
cout << "Address of var2: " << &var2 << endl;
// print address of var3
cout << "Address of var3: " << &var3 << endl;
}
// print address of var1 cout << "Address of var1: "<< &var1 << endl; // print address of var2 cout << "Address of var2: " << &var2 << endl; // print address of var3 cout << "Address of var3: " << &var3 << endl; }
// print address of var1
cout << "Address of var1: "<< &var1 << endl;

// print address of var2
cout << "Address of var2: " << &var2 << endl;

// print address of var3
cout << "Address of var3: " << &var3 << endl;
}

Output

Address of var1: 0x7fff5fbff8ac
Address of var2: 0x7fff5fbff8a8
Address of var3: 0x7fff5fbff8a4

The hexadecimal form of the address is indicated by the initial 0x in this case.

Keep in mind that the first address is 4 bytes different from the second, and the second address is 4 bytes different from the third.

This is due to the fact that an int variable in a 64-bit system has a size of 4 bytes.

Note: If you run the program, you might not obtain the same outcomes.

C++ Pointers

Pointers are used to hold addresses rather than values, as was already mentioned.

This is how pointers can be declared.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int *pointVar;
int *pointVar;
int *pointVar;

Here, an int-type pointer named pointVar has been declared.

The following is another way that pointers can be declared.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int* pointVar; // preferred syntax
int* pointVar; // preferred syntax
int* pointVar; // preferred syntax

Let’s look at another declaration of pointers example.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int* pointVar, p;
int* pointVar, p;
int* pointVar, p;

Here, a pointer named pointVar and a regular variable named p has been declared.

For the purpose of declaring pointers, the * operator is used after the data type.

Giving Pointers Addresses

Here’s how we can give pointers addresses:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int* pointVar, var;
var = 5;
// assign address of var to pointVar pointer
pointVar = &var;
int* pointVar, var; var = 5; // assign address of var to pointVar pointer pointVar = &var;
int* pointVar, var;
var = 5;

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

The variable var in this case has the value 5. Additionally, the code pointVar = &var assigns the address of var to the pointVar pointer.

Utilizing Pointers, obtain the Value from the Address

The * operator is used to retrieve the value pointed to by a pointer. For instance:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int* pointVar, var;
var = 5;
// assign address of var to pointVar
pointVar = &var;
// access value pointed by pointVar
cout << *pointVar << endl; // Output: 5
int* pointVar, var; var = 5; // assign address of var to pointVar pointVar = &var; // access value pointed by pointVar cout << *pointVar << endl; // Output: 5
int* pointVar, var;
var = 5;

// assign address of var to pointVar
pointVar = &var;

// access value pointed by pointVar
cout << *pointVar << endl;   // Output: 5

The address of var is given to pointVar in the code above. To retrieve the value kept at that address, we used the *pointVar.

The dereference operator is the term used to describe the use of * with pointers. It uses a pointer as its primary mechanism and outputs the value indicated by the address saved in the pointer. In other words, *pointVar = var

Note that pointVar and *pointVar are entirely different in C++. We are unable to use the syntax *pointVar = &var;.

Example 2: Working of C++ Pointers

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
int var = 5;
// declare pointer variable
int* pointVar;
// store address of var
pointVar = &var;
// print value of var
cout << "var = " << var << endl;
// print address of var
cout << "Address of var (&var) = " << &var << endl
<< endl;
// print pointer pointVar
cout << "pointVar = " << pointVar << endl;
// print the content of the address pointVar points to
cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar << endl;
return 0;
}
#include <iostream> using namespace std; int main() { int var = 5; // declare pointer variable int* pointVar; // store address of var pointVar = &var; // print value of var cout << "var = " << var << endl; // print address of var cout << "Address of var (&var) = " << &var << endl << endl; // print pointer pointVar cout << "pointVar = " << pointVar << endl; // print the content of the address pointVar points to cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar << endl; return 0; }
#include <iostream>
using namespace std;
int main() {
    int var = 5;

    // declare pointer variable
    int* pointVar;

    // store address of var
    pointVar = &var;

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

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

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

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

Output

var = 5
Address of var (&var) = 0x61ff08
pointVar = 0x61ff08
Content of the address pointed to by pointVar (*pointVar) = 5

Value Pointed by Pointers Changing In C++

If pointVar points to var’s address, we can use *pointVar to modify var’s value.

For instance,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int var = 5;
int* pointVar;
// assign address of var
pointVar = &var;
// change value at address pointVar
*pointVar = 1;
cout << var << endl; // Output: 1
int var = 5; int* pointVar; // assign address of var pointVar = &var; // change value at address pointVar *pointVar = 1; cout << var << endl; // Output: 1
int var = 5;
int* pointVar;

// assign address of var
pointVar = &var;

// change value at address pointVar
*pointVar = 1;

cout << var << endl; // Output: 1

We can modify the value of var by using *pointVar if pointVar points to the address of var.

For illustration

Example 3: Changing Value Pointed by Pointers In C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
using namespace std;
int main() {
int var = 5;
int* pointVar;
// store address of var
pointVar = &var;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;
cout << "Changing value of var to 7:" << endl;
// change value of var to 7
var = 7;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;
cout << "Changing value of *pointVar to 16:" << endl;
// change value of var to 16
*pointVar = 16;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << endl;
return 0;
}
#include <iostream> using namespace std; int main() { int var = 5; int* pointVar; // store address of var pointVar = &var; // print var cout << "var = " << var << endl; // print *pointVar cout << "*pointVar = " << *pointVar << endl << endl; cout << "Changing value of var to 7:" << endl; // change value of var to 7 var = 7; // print var cout << "var = " << var << endl; // print *pointVar cout << "*pointVar = " << *pointVar << endl << endl; cout << "Changing value of *pointVar to 16:" << endl; // change value of var to 16 *pointVar = 16; // print var cout << "var = " << var << endl; // print *pointVar cout << "*pointVar = " << *pointVar << endl; return 0; }
#include <iostream>
using namespace std;
int main() {
    int var = 5;
    int* pointVar;

    // store address of var
    pointVar = &var;

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

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

    cout << "Changing value of var to 7:" << endl;

    // change value of var to 7
    var = 7;

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

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

    cout << "Changing value of *pointVar to 16:" << endl;

    // change value of var to 16
    *pointVar = 16;

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

    // print *pointVar
    cout << "*pointVar = " << *pointVar << endl;
    return 0;
}

Output

var = 5
*pointVar = 5
Changing value of var to 7:
var = 7
*pointVar = 7
Changing value of *pointVar to 16:
var = 16
*pointVar = 16

Common errors made when using pointers In C++

Let’s say we want a pointer variable. Point to point to var’s address Then,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int var, *varPoint;
// Wrong!
// varPoint is an address but var is not
varPoint = var;
// Wrong!
// &var is an address
// *varPoint is the value stored in &var
*varPoint = &var;
// Correct!
// varPoint is an address and so is &var
varPoint = &var;
// Correct!
// both *varPoint and var are values
*varPoint = var;
int var, *varPoint; // Wrong! // varPoint is an address but var is not varPoint = var; // Wrong! // &var is an address // *varPoint is the value stored in &var *varPoint = &var; // Correct! // varPoint is an address and so is &var varPoint = &var; // Correct! // both *varPoint and var are values *varPoint = var;
int var, *varPoint;

// Wrong! 
// varPoint is an address but var is not
varPoint = var;

// Wrong!
// &var is an address
// *varPoint is the value stored in &var
*varPoint = &var;

// Correct! 
// varPoint is an address and so is &var
varPoint = &var;

 // Correct!
// both *varPoint and var are values
*varPoint = var;

you may like:

Pointers and Arrays In C++

Leave a Reply