C ++ pointers
Learning C ++ pointers easy and fun. Through a pointer, you can simplify the implementation of some of the C ++ programming tasks, there are some tasks, such as dynamic memory allocation, no pointer is unenforceable. So, I want to become a good C ++ programmer, learning pointers are necessary.
As you know, each variable has a memory location, each memory location defines the address-of operator access can use the hyphen (&), which represents an address in memory. Consider the following examples, which will define the output variable address:
#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 results:
var1 变量的地址: 0xbfebd5c0 var2 变量的地址: 0xbfebd5b6
By way of example above, we understand what a memory address and how to access it. Let's look at what is a pointer.
What is a pointer?
A pointer is a variable whose value is the address of another variable, namely, the direct address of the memory location.Before Like other variables or constants, you must use a pointer memory address of other variables, it is declared. The general form of a pointer variable declaration is:
type *var-name;
Here, type is a pointer to the base type, it must be a valid C ++ data types,var-name is the name of the pointer variable. Is used to declare a pointer asterisk * multiplication use the asterisk is the same. However, in this statement, the asterisk is used to specify a variable is a pointer. The following is a valid pointer declaration:
int *ip; /* 一个整型的指针 */ double *dp; /* 一个 double 型的指针 */ float *fp; /* 一个浮点型的指针 */ char *ch /* 一个字符型的指针 */
All pointers actual data type of the value, whether it is an integer, float, string, or other data types are the same, is a hexadecimal number represents a long memory address. The only difference between the different types of data pointer, the pointer is a variable or constant data types.
Using pointers in C ++
Will frequently use the pointer when the following actions: define a pointer variable, the variable address assigned to the pointer, access the value of the pointer variable available addresses. This is done by using the unary* operator to return the value of the variable is located in the operand address specified.The following examples relate to these actions:
#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 results:
Value of var variable: 20 Address stored in ip variable: 0xbfc601ac Value of *ip variable: 20
C ++ pointer Explanation
In C ++, there are many pointers related concepts that are very simple, but very important. The following lists the C ++ programmer must be aware of some important concepts associated with the pointer:
概念 | 描述 |
---|---|
C++ Null 指针 | C++ 支持空指针。NULL 指针是一个定义在标准库中的值为零的常量。 |
C++ 指针的算术运算 | 可以对指针进行四种算术运算:++、--、+、- |
C++ 指针 vs 数组 | 指针和数组之间有着密切的关系。 |
C++ 指针数组 | 可以定义用来存储指针的数组。 |
C++ 指向指针的指针 | C++ 允许指向指针的指针。 |
C++ 传递指针给函数 | 通过引用或地址传递参数,使传递的参数在调用函数中被改变。 |
C++ 从函数返回指针 | C++ 允许函数返回指针到局部变量、静态变量和动态内存分配。 |