Happy file C++ pointer

C++ pointers

Table of contents

C++ pointers

example

What are pointers?

Using pointers in C++

example

Detailed explanation of C++ pointers



 

Learning pointers in C++ is easy and fun. The performance of some C++ programming tasks can be simplified through pointers, 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 every memory location defines an address that can be accessed using the hyphen (&) operator, which represents an address in memory. Please see the following example, which will output the address of the variable defined:

example

#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 result:


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

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

What are pointers?

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 a pointer variable declaration is:


type *var-name;

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


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

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

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 available address in the pointer variable. These are returned by using the unary operator  *  to return the value of the variable at the address specified by the operand. The following examples involve these operations:

example

#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 result:


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

Detailed explanation of C++ pointers

In C++, there are many concepts related to pointers. These concepts are very simple, but they are all important. Some important pointer-related concepts that C++ programmers must be aware of are listed below:

concept describe
C++ Null Pointer C++ supports null pointers. The NULL pointer is a constant defined in the standard library with the value zero.
Arithmetic operations on C++ pointers Four arithmetic operations can be performed on pointers: ++, --, +, -
C++ pointers vs arrays There is a close relationship between pointers and arrays.
C++ array of pointers Arrays can be defined to store pointers.
C++ pointer to pointer C++ allows pointers to pointers.
C++ pass pointer to function Pass parameters by reference or address, so that the passed parameters are changed in the calling function.
C++ return pointer from function C++ allows functions to return pointers to local variables, static variables, and dynamic memory allocation.

Guess you like

Origin blog.csdn.net/weixin_72686492/article/details/129988648