Hello, I’m Yue Chuang.

Learning C++ Pointers is easy and fun. Pointers make it easier to perform some C++ programming tasks, and some tasks, such as dynamic memory allocation, cannot be performed without Pointers. So, to be a good C++ programmer, learning Pointers is essential.

As you know, each variable has a memory location, and each memory location defines an address accessible using the hyphen (&) operator, which represents an address in memory. Look at the following example, which prints the address of the defined variable:

#include <iostream>
 
using namespace std;
 
int main (a)
{
   int  var1;
   char var2[10];
 
   cout << "Address of variable var1:";
   cout << &var1 << endl;
 
   cout << "Address of variable var2:";
   cout << &var2 << endl;
 
   return 0;
}
Copy the code

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

Address of variable var1:0x6ffe1cVar2 variable address:0x6ffe10
Copy the code

From the above example, we learned what a memory address is and how to access it. Now 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. As with other variables or constants, you must declare them before using Pointers to store the addresses of other variables. The general form of a pointer variable declaration is:

type *var-name;
// type* pointerVarName
Copy the code

In this case, 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 a 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 are valid pointer declarations:

int    *ip;    /* A pointer to an integer */
double *dp;    /* A pointer of type double */
float  *fp;    /* A pointer to a floating point */
char   *ch;    /* A character pointer */
Copy the code

The actual data type of the value of all Pointers, whether integer, float, character, or any other data type, is the same: 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 to which the pointer points is different. (That is, the data pointed to is different)

Pointers are used in C++

The following operations are frequently performed when using Pointers:

  • Define a pointer variable
  • Assign the address of a variable to a pointer
  • Access the value of the available address in a pointer variable

These return the value of the variable at the address specified by the operand by using the unary operator *. The following example involves these operations:

#include <iostream>
 
using namespace std;
 
int main (a)
{
   int  var = 20;   // Declare the actual variable
   int  *ip;        // Declare a pointer variable
 
   ip = &var;       // Store the address of var in the pointer variable
 
   cout << "Value of var variable: ";
   cout << var << endl;
 
   // Prints 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;
}
Copy the code

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

Value of var variable: 20
Address stored in ip variable: 0x6ffe14
Value of *ip variable: 20
Copy the code

AI yue chuang · launched tutorials, including “Python language tutorials, C++ tutorials, algorithm/data structure tutorials”, all one-to-one teaching: one-to-one tutoring + one-to-one q&a + assignment + project practice, etc. QQ, wechat online, at any time to respond! V: Jiabcdefh