C++ Pointer

Learning C++ pointers is both simple and fun. Through pointers, some C++ programming tasks can be simplified, and some tasks, such as dynamic memory allocation, cannot be performed without pointers. Therefore, to become a good 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 ampersand (&) operator, which represents an address in memory. See the following example, which will output the addresses of the defined variables:

Example

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

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

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

Through the above example, we have learned what a memory address is and how to access it. Next, let's take a look at what a pointer is.

What is a pointer?

PointerIt is a variable whose value is the address of another variable, that is, the direct address of a memory location. Just like other variables or constants, 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,typeIt is the base type of the pointer, and it must be a valid C++ data type.var-name. The first represents an array whose elements are int pointers. The second represents a pointer to an int array. When encountering such problems, read from the inside out.

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

The actual data type of the value of all pointers, whether integer, floating-point, character, or other data types, is the same: a long hexadecimal number representing a memory address. The only difference between pointers of different data types is the data type of the variable or constant to which the pointer points.

Using Pointers in C++

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

Example

#include <iostream> using namespace std; int main () { int var = 20; // Declaration of the actual variable int *ip; // Declaration of the pointer variable 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 at the address stored 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++ Pointers in Detail

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

ConceptsDescription
C++ Null PointerC++ supports null pointers. A NULL pointer is a constant defined in the standard library with a value of zero.
C++ Pointer ArithmeticFour arithmetic operations can be performed on pointers: ++, --, +, -
C++ Pointers vs ArraysThere is a close relationship between pointers and arrays.
C++ Pointer ArrayArrays can be defined to store pointers.
C++ Pointer to PointerC++ allows pointers to pointers.
C++ Passing Pointers to FunctionsPassing parameters by reference or by address makes the passed parameters changed in the calling function.
C++ Returning Pointers from FunctionsC++ allows functions to return pointers to local variables, static variables, and dynamically allocated memory.
other extensions