Cplusplus 简明教程
C++ Pointers
C 指针很容易学而且很有趣。某些 C 任务使用指针更容易执行,而其他 C++ 任务(例如动态内存分配)则无法在没有它们的情况下执行。
C pointers are easy and fun to learn. Some C tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them.
正如您所知,每个变量都是一个内存位置,每个内存位置都有其定义的地址,该地址可以使用表示内存中地址的&运算符访问。考虑以下内容,它将打印定义的变量的地址:
As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. Consider the following which will print the address of the variables defined −
#include <iostream>
using namespace std;
int main () {
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Address of var1 variable: 0xbfebd5c0
Address of var2 variable: 0xbfebd5b6
What are Pointers?
pointer 是一个变量,其值是另一个变量的地址。像任何变量或常量一样,你必须在使用指针之前声明一个指针。指针变量声明的一般形式是 −
A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is −
type *var-name;
在此, type 是指针的基础类型;它必须是有效的 C++ 类型, var-name 是指针变量的名称。你用于声明指针的星号与你用于乘法的星号相同。然而,在此语句中,星号用于将变量指定为指针。以下是有效的指针声明 −
Here, type is the pointer’s base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid pointer declaration −
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
所有指针的值的实际数据类型,无论是整数、浮点数、字符还是其他类型,都是相同的,即表示内存地址的长十六进制数。不同数据类型的指针之间的唯一差异是指针指向的变量或常量的类型。
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
Using Pointers in C++
有一些重要的操作,我们将非常频繁地对指针执行这些操作。 (a) 我们定义了一个指针变量。 (b) 将变量的地址分配给指针。 (c) 最后,访问指针变量中提供的地址的值。这是通过使用一元运算符 * 来完成的,该运算符返回位于其操作数指定的地址处变量的值。以下示例使用了这些操作 −
There are few important operations, which we will do with the pointers very frequently. (a) We define a pointer variable. (b) Assign the address of a variable to a pointer. (c) Finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations −
#include <iostream>
using namespace std;
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
当上述代码被编译并执行时,它会产生类似以下结果 −
When the above code is compiled and executed, it produces result something as follows −
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Pointers in C++
指针有许多简单易懂的概念,它们对 C 编程非常重要。有以下几个重要的指针概念,对于 C 程序员来说应该非常清楚 −
Pointers have many but easy concepts and they are very important to C programming. There are following few important pointer concepts which should be clear to a C programmer −
Sr.No |
Concept & Description |
1 |
Null PointersC++ supports null pointer, which is a constant with a value of zero defined in several standard libraries. |
2 |
Pointer ArithmeticThere are four arithmetic operators that can be used on pointers: ++, --, +, - |
3 |
Pointers vs ArraysThere is a close relationship between pointers and arrays. |
4 |
Array of PointersYou can define arrays to hold a number of pointers. |
5 |
Pointer to PointerC++ allows you to have pointer on a pointer and so on. |
6 |
Passing Pointers to FunctionsPassing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function. |
7 |
Return Pointer from FunctionsC++ allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well. |