catalogue
Concept and understanding of pointer
Arithmetic operation of pointer
Hi, here comes my decomposing machine 226 again. This time, let's talk about the pointer
Pointer is a core concept in C + +. It allows programmers to operate memory directly. It can be said that pointer is a tool. Next, we use pointers to traverse the array.
//Pointer traversal array //Author: decomposing machine #include <iostream> using namespace std; int main() { int boli[] = { 5,2,0,1,3,1,4 };//Define an array containing elements 5201314 int* daijie = boli;//The array element is initialized with the array element name, where the first pointer represents the array element name for (int i = 0; i < 7; i++) { cout << *(daijie++) << '\t';// } cout << endl; return 0; }
Operation results
Concept and understanding of pointer
A pointer is something that points to an object, and in a program is something that points to the address of data. The memory of a computer can be regarded as a closely arranged data sequence. Next to each small piece of data (that is, a byte), there is a number representing the address of the data,
Assuming that the address of boli is 203, the following elements are incremented in turn (in fact, because the array in the example is int type, the real address is incremented by 4 bytes in turn). The pointer is the memory address, so boli is 203. If you want to obtain the value under the address, you can use * boli to obtain it. Let's look at the following code
//Pointer traversal array //Author: decomposing machine #include <iostream> using namespace std; int main() { int boli[] = { 5,2,0,1,3,1,4 };//Define an array containing elements 5201314 int* daijie = boli;//This statement initializes the pointer with an array name, where the array name represents the address of the first element of the array for (int i = 0; i < 7; i++) { cout << *(daijie++) << '\t'; cout << "address" << daijie << endl; } cout << endl; return 0; }
Operation results
When we print daijie directly, the address of the element is displayed. Indicates that the pointer is the address
Pointer creation
"Small score code"
//Pointer traversal array //Author: decomposing machine #include <iostream> using namespace std; int main() { float * boli = NULL; string * boli1; int * boli2,* boli3; int* boli4, boli5;//boli5 is just an integer return 0; }
Generally, we will initialize the pointer to NULL, also known as NULL pointer, which provides us with a unified and manageable exception value. If the pointer is not initialized, it may point to an unknown address, then we will cause the program crash when trying to read the value. In addition, we cannot assign a value to the pointer with a shape other than 0 during pointer initialization.
In addition to the above pointer types, C + + also has a general void * pointer. We know that the pointer is the address. The type of pointer just represents the data type stored at the location pointed to by the address. If we replace the int * pointer with the float * pointer, the program just redefines the data as the floating point. Therefore, void * here only represents an address and does not know the data type it represents,
Basic operation of pointer
//Pointer basic operation //Author: decomposing machine #include <iostream> using namespace std; int main() { int boli = 4; int* daijie = &boli; cout << "boli Your address is" << &boli << endl; cout << "The value of the pointer is" << daijie << endl; if (daijie) { cout << "The number indicated by the pointer is" << *daijie << endl; } return 0; }
Operation results
We can see that the symbol & indicates the address taking operation. It can obtain the address of the variable, assign it to daijie, print boli and daijie, and obtain the value of boli at the same time.
In the example, we also added a condition to judge whether the pointer is NULL, so as to ensure that the dereference of daijie must be safe. Here, we use the implicit conversion between numeric value and Boolean value, and only write daijie as a condition, because daijie will be converted to false when it is empty.
Lvalue dereference
//Pointer basic operation //Author: decomposing machine #include <iostream> using namespace std; int main() { int boli = 520; int* daijie = &boli; if (daijie) { cout << "The number indicated by the pointer is" << *daijie << endl; cout << "boli The value of is" << boli << endl; *daijie = 1314; cout << "The changed pointer value is" << *daijie << endl; cout << "boli The value of is" << boli << endl; } return 0; }
Operation results
In this example, we use the left value dereference operation to change the value in the pointer to boli to 1314. Since the pointer is the same as the address of boli, boli will also become 1314,
Basic operation of graphic pointer
The variable boli in the figure consists of two parts. The upper part "daijie" is the address and the lower part "* daijie" is the data in the address. If the address of daijie is 100, * daije will return data 520. The & boli in the upper right corner obtains the address 100 of the variable and assigns it to a pointer variable similar to daijie, * daijie has obtained the permission to modify boli here.
Arithmetic operation of pointer
Pointers can perform some arithmetic operations like integers, and can also modify addresses,
//Arithmetic operation of pointer //Author: decomposing machine #include <iostream> using namespace std; int main() { int boli[] = { 5,2,0,1,3,1,4 }; int* daijie = boli; cout << "boli+4:" << *(boli + 4) << endl; cout << "daijie+4 :" << *(daijie + 4) << endl; cout << "daijie:" << daijie << endl; cout << "boli:" << boli << endl; return 0; }
Operation results
The array name can actually be regarded as a pointer to the first tuple of the array. Various operations of the pointer are used for the array name, but the array name cannot be re assigned because the array is static.
Finally finished writing today's blog