English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Pointer

In this article, you will learn everything about pointers. You will learn how to store values in a computer and how to access them using pointers.

Pointers are C++The powerful features of it, which make C++Distinguishes itself from other programming languages (such as Java and Python).

Pointers in C ++Used in the program to access memory and manipulate addresses.

C ++Address

To understand pointers, you first need to know how to store data on a computer.

Each variable created in the program allocates a location in the computer's memory. The value stored by the variable is actually stored in the specified location.

To know where the data is stored, C++There is an & operator. The operator & (reference) gives the address occupied by the variable.

If var is a variable, then &var gives the address of the variable.

Example1: C ++address

#include <iostream>
using namespace std;
int main()
{
    int var1 = 3;
    int var2 = 24;
    int var3 = 17;
    cout << &var1 << endl;
    cout << &var2 << endl;
    cout << &var3 << endl;
}

Output result

0x7fff5fbff8ac
0x7fff5fbff8a8
0x7fff5fbff8a4

Note: You may not get the same result on your system.

The leading 0x indicates that the address is in hexadecimal format.

Please note that the first address is separated from the second address by4bytes, the second address is separated from the third address by4bytes.

This is because in64In a bit system, the size of an integer (a variable of type int) is4bytes.

Pointer variables

C ++Enables you to directly manipulate data in the computer's memory. You can allocate and deallocate any space in memory as needed. This is done using the Pointer (pointer) variable.

A pointer variable is a variable that points to a specific address in memory where another variable is pointed to.

How to declare a pointer?

int *p;
      or
int* p;

The above statement defines a pointer variable p. It stores the memory address

The asterisk (*) is the dereference operator, indicatingThe pointer to which it points.

In this case, the pointer p is a pointer to int, meaning it points to the integer value in the memory address.

The reference operator (&) and the reference operator (*)

As described above, the reference operator (&) gives the address of the variable.

To get the value stored in the memory address, we use the dereference operator (*).

For example: If the number variable is stored in memory address0x123contains a value5.

reference(&) operator gives a value0x123, while dereference (*) operator gives a value5.

Note:Note: c++Used in pointer declaration (*) symbol is not a reference pointer. It is just a symbol similar to creating a pointer.

Example2: C ++Pointer

C ++The program demonstrates the operation of pointers.

#include <iostream>
using namespace std;
int main() {
    int *pc, c;
    
    c = 5;
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;
    pc = &c;    // Pointer pc saves the memory address of variable c
    cout << "The pointer address held by pc (pc): " << pc << endl;
    cout << "The value held by the address pointer pc is (*pc): " << *pc << endl << endl;
    
    c = 11;    // The value in memory address &c from5changed to11.
    cout << "The address held by the address pointer pc is (pc): " << pc << endl;
    cout << "The content held by the address pointer pc is (*pc): " << *pc << endl << endl;
    *pc = 2; 
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;
    return 0;
}

Output result

Address of c (&c): 0046FD44
Value of c (c): 5
The pointer address held by pc (pc): 0046F
The value held by the address pointer pc is (*pc): 5
The address held by the address pointer pc is (pc): 0046FD4
The content held by the address pointer pc is (*pc):
Address of c (&c): 0046FD44
Value of c (c): 2

Program usage instructions

  • when c = 5; value5stored in the variable c-0x7fff5fbff8stored in the address of c.

  • when pc = &c; the address of pointer pc is c-0x7fff5fbff8when c, the expression (dereference operator)*pc outputs the value stored at that address5.

  • when c = 11; Since the address pointer held by pc is the same as c - 0x7fff5fbff8c is the same, so when executing the expression*pc, the value of c will also change, now output11.

  • When*pc = 2when; it changes pc - 0x7fff5fbff8the content stored in the address where c is stored. This from11changed to2. Therefore, when we output the value of c, this value is also2.

Common mistakes when using pointers

Assuming you want the pointer pc to point to the address of c, then,

int c; *pc;
pc = c;  /* Error! pc is an address, but c is not an address. */
*pc = &c; /*Incorrect!  * pc is the value pointed to by the address, and &c is the address.*/
pc = &c; /* Correct!     pc is an address, and &c is also an address. */
*pc = c; /* Correct!  * pc is the value pointed to by the address, and c is also a value. */

In both cases, the pointer pc is not pointing to the address of c.

You should also check out these tutorials related to pointers: