C++ Pointers

Learning C++ pointers is both simple and fun. With pointers, you can simplify the execution of some C++ programming tasks, as well as some tasks, such as dynamic memory allocation. No pointers can't be executed. Therefore, to become a good C++ programmer, learning pointers is necessary.

As you know, each variable has a memory location, and each memory location defines a hyphen ( &)The address that the operator accesses, which represents an address in memory. Take a look at the example below, which will output the defined variable address:

Every variable is a memory location, which has its address defined.

This outputs the memory address, which stores the variable score.


#include <iostream> using namespace std; int main () { int var1; char var2[10]; cout << "var1 Variable address: "; cout << &var1 << endl; cout << "var2 Variable address: "; cout << &var2 << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

var1 Address of the variable: 0xbfebd5c0
var2 Variable address: 0xbfebd5b6

With the above example, we learned what a memory address is and how to access it. Let's see what is a pointer.

What is a pointer?

Pointer is a variable whose value is the address of another variable, that is, the direct address of the memory location. Just like other variables or constants, you must declare them before using them to store addresses of other variables. The general form of pointer variable declaration is:

type *var-name;

Here, type is the base type of the pointer, it must be a valid C++ data type, and var-name is the name of the pointer variable. The asterisk used to declare the pointer * is the same as the asterisk used in multiplication. However, in this statement, the asterisk is used to specify that a variable is a pointer. The following is a valid pointer declaration:

int *ip; /* Integer pointer*/
double * Dp; /* A double pointer*/
float * Fp; /* A floating point pointer*/
char * Ch; /* A character pointer*/

The actual data type of all pointer values, whether integer, float, character, or other data type, is the same, a long hexadecimal number representing the memory address. The only difference between pointers of different data types is that the data or type of the variable or constant pointed to by the pointer is different.

Using pointers in C++

When a pointer is used, the following operations are frequently performed: defining a pointer variable, assigning the variable address to the pointer, and accessing the value of the available address in the pointer variable. These are the values ​​of the variable at the address specified by the operand by using the unary operator *. The following examples involve these operations:


#include <iostream> using namespace std; int main () { int var = 20; // Statement of actual variables int *ip; // Pointer variable declaration ip = &var; // Store the address of var in the pointer variable cout << "Value of var variable: "; cout << var << endl; // Output the address stored in the pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // Access the value of the address in the pointer cout << "Value of *ip variable: "; cout << *ip << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20

C++ pointer details

In C++, there are a lot of pointer-related concepts that are simple but important. Here are some important concepts related to pointers that C++ programmers must be aware of:

< Td> Pass a parameter by reference or address so that the passed parameter is changed in the calling function. < Td>C++ allows functions to return pointers to local variables, static variables, and dynamic memory allocation.
C++ Null PointerC++ supports null pointers. A NULL pointer is a constant that defines a value of zero in the standard library.
C++ pointer arithmetic can be The pointer performs four arithmetic operations: ++, --, +, -
C++ pointer vs array pointer and There is a close relationship between arrays.
C++ pointer array can be defined to be used An array of stored pointers.
C++ pointer to pointer C++ allows pointers to pointers.
C++ passing pointers to functions
C++ return pointer from function