C++ Pointers
Learning C++ pointers is both simple and interesting. Through pointers, you can simplify the execution of some C++ programming tasks, and some tasks, such as dynamic memory allocation, cannot be performed without pointers. Therefore, to become an excellent C++ programmer, learning pointers is essential.
As you know, every variable has a memory location, and each memory location defines an address accessible using the ampersand (&) operator, which represents an address in memory. See the example below, which outputs 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 code above is compiled and executed, it produces the following result:
var1 变量的地址: 0xbfebd5c0
var2 变量的地址: 0xbfebd5b6
Through the example above, we understand what a memory address is and how to access it. Next, let us look at what a pointer is.
What is a Pointer?
pointeris a variable whose value is the address of another variable, that is, the direct address of a memory location. Like other variables or constants, you must declare the 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,var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same as the one 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; /* 一个整型的指针 */
double *dp; /* 一个 double 型的指针 */
float *fp; /* 一个浮点型的指针 */
char *ch; /* 一个字符型的指针 */
The actual data type of all pointer values, 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 the pointer points to.
Using Pointers in C++
When using pointers, you frequently perform the following operations: defining a pointer variable, assigning a variable's address to the pointer, and accessing the value at the available address in the pointer variable. These are done using the unary operator * to return the value of the variable at the address specified by the operand. The example below 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 in the pointer
cout
<<
"
Value of *ip variable:
"
;
cout
<< *
ip
<<
endl
;
return
0
;
}
When the code above 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
C++ Pointers in Detail
In C++, there are many pointer-related concepts, which are simple but important. The following lists some important pointer-related concepts that C++ programmers must be clear about:
| Concept | Description |
|---|---|
| C++ Null Pointers | C++ supports null pointers. A NULL pointer is a constant defined in the standard library with a value of zero. |
| C++ Pointer Arithmetic | Four arithmetic operations can be performed on pointers: ++, --, +, |
| C++ Pointers vs Arrays | There is a close relationship between pointers and arrays. |
| C++ Arrays of Pointers | You can define arrays used to store pointers. |
| C++ Pointer to Pointer | C++ allows a pointer to a pointer. |
| C++ Passing Pointers to Functions | Passing parameters by reference or address allows the passed parameters to be changed in the called function. |
| C++ Returning a Pointer from a Function | C++ allows functions to return pointers to local variables, static variables, and dynamically allocated memory. |