Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The C++ pointer


May 11, 2021 C++


Table of contents


The pointer for C+

It's easy and fun to learn the pointers for C+. P ointers simplify the execution of some C++ programming tasks, as well as tasks such as dynamic memory allocation, which cannot be performed without pointers. Therefore, it is necessary to learn the pointer in the interests of being a good C++ programmer.

As you know, each variable has a memory location, and each memory location defines an address that can be accessed using the lyse operator, which represents an address in memory. Look at the following example, which outputs 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 produces the following results:

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

From the example above, we learned what a memory address is and how to access it. Let's take a look at what a pointer is.

What is a pointer?

A pointer is a variable whose value is the address of another variable, that is, the direct address of the memory location. L ike other variables or constants, you must declare other variable addresses before you can store them with pointers. Pointer variable declarations are generally in the form of:

type *var-name;

Here, type is the base type of the pointer, it must be a valid C++ data type, var-name is the name of the pointer variable. T he asterisk used to declare the pointer is the same as the asterisk used in multiplication. H owever, in this statement, the asterisk is used to specify that a variable is a pointer. Here's a valid pointer declaration:

int    *ip;    /* 一个整型的指针 */
double *dp;    /* 一个 double 型的指针 */
float  *fp;    /* 一个浮点型的指针 */
char   *ch     /* 一个字符型的指针 */

The actual data types of the values of all pointers, whether integer, floating-point, character type, or any other data type, are the same, representing a long heteens of memory addresses. The only difference between pointers of different data types is that the pointers point to different data types for variables or constants.

The pointer is used in C+

When using pointers, you frequently do the following: define a pointer variable, assign a variable address to a pointer, and access the value of the available address in the pointer variable. T hese are the values of variables that * address specified by the operans by using the u-operator . The following examples relate to 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 produces the following results:

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

The C++ pointer is explained in detail

There are a lot of pointer-related concepts in C+ that are simple, but important. Here are some important pointer-related concepts that programmers must be aware of:

concept describe
C ++ NULL pointer C ++ supports empty pointer.NULL pointer is a constant defined in a value of zero in the standard library.
C ++ pointer arithmetic operation Four arithmetic operations can be performed on the pointer: ++, -, +,
C ++ pointer VS array There is a close relationship between the pointers and arrays.
C ++ pointer array You can define an array used to store pointers.
C ++ points to pointers C ++ allows points to point to pointers of the pointer.
C ++ conveying pointer to function By reference or address delivery parameters, the transferred parameters are changed in the call function.
C ++ returns a pointer from function C ++ allows the function to return the pointer to local variables, static variables, and dynamic memory allocation.