C++ pointers


Learning pointers in C++ is easy and fun. Pointers simplify the execution of some C++ programming tasks, and some tasks, such as dynamic memory allocation, cannot be performed without pointers. Therefore, if you want to become an excellent C++ programmer, it is necessary to learn pointers.

As you know, every variable has a memory location, and each memory location defines an address that can be accessed using the hyphen (&) operator, which represents an address in memory. . Please look at the following example, which will output the defined variable address:

#include <iostream>

using namespace std;

int main ()
{
   int  var1;
   char var2[10];

   cout << "var1 变量的地址: ";
   cout << &var1 << endl;

   cout << "var2 变量的地址: ";
   cout << &var2 << endl;

   return 0;
}

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

var1 变量的地址: 0xbfebd5c0
var2 变量的地址: 0xbfebd5b6

Through the above example, we Learn what a memory address is and how to access it. Next let's look at what pointers are.

What is a pointer?

A pointer is a variable whose value is the address of another variable, that is, the direct address of a memory location. Just like any other variable or constant, you must declare a pointer before using it to store the address of another variable. The general form of pointer variable declaration is:

type *var-name;

Here, type is the base type of the pointer, which must be a valid C++ data type, var-name is the name of the pointer variable. The asterisk * used to declare pointers is the same as 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;    /* 一个整型的指针 */
double *dp;    /* 一个 double 型的指针 */
float  *fp;    /* 一个浮点型的指针 */
char   *ch     /* 一个字符型的指针 */

The actual data types of all pointer values, whether integer, floating point, character, or other data types, are the same and represent the same Long hexadecimal number of memory address. The only difference between pointers of different data types is the data type of the variable or constant pointed to by the pointer.

Using pointers in C++

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

#include <iostream>

using namespace std;

int main ()
{
   int  var = 20;   // 实际变量的声明
   int  *ip;        // 指针变量的声明

   ip = &var;       // 在指针变量中存储 var 的地址

   cout << "Value of var variable: ";
   cout << var << endl;

   // 输出在指针变量中存储的地址
   cout << "Address stored in ip variable: ";
   cout << ip << endl;

   // 访问指针中地址的值
   cout << "Value of *ip variable: ";
   cout << *ip << endl;

   return 0;
}

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

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

C++ Pointer Detailed

In C++ , there are many pointer-related concepts, these concepts are very simple, but they are all important. Listed below are some important pointer-related concepts that C++ programmers must know:

ConceptDescription
C++ Null PointerC++ supports null pointers. The NULL pointer is a constant defined in the standard library with a value of zero.
C++ pointer arithmetic operations Four arithmetic operations can be performed on pointers: ++, --, +, -
C++ Pointer vs ArrayThere is a close relationship between pointers and arrays.
C++ pointer arrayYou can define an array used to store pointers.
C++ Pointers to pointersC++ allows pointers to pointers.
C++ Pass pointer to functionPass parameters by reference or address so that the passed parameters are changed in the calling function.
C++ Returning pointers from functions C++ allows functions to return pointers to local variables, static variables and dynamic memory allocation.