Statement: Please indicate the source for reprinting!
After learning the C language, I have read the pointer for countless times. Sometimes I feel that I have understood it, And the pointer is the address, but I am confused when I read the code, recently, we have made a little more effort on Pointer enhancement.
In most cases, pointers in the program are confused because they do not understand the pointer:
First, as an identifier, the variable is defined as a pointer, for example, int * P; // It only indicates that variable P is a pointer variable without practical significance.
Second, as an operator, like the +,-, and operator, such as cout <* P; // output the content pointed to by the pointer Variable P.
Generally, in a program, the first occurrence of the pointer * is used as an identifier to declare the pointer variable, followed by an operator, but not necessarily, mainly depends on the context of the program.
The "&" symbol is a unary operator, and the variable address is used.
Well, the most commonly used pointer is passed as a parameter. Due to my research on image processing, the program often needs to process thousands of images, and the memory often overflows. Memory Allocation is especially important. At this time, I was forced to learn how to use pointers.
Object-Oriented programming function transfer is often a value transfer, but the passing parameter is a copy of the real parameter. If I use the value transfer, thousands of images can be copied in the memory, therefore, pointer transmission is considered.
// Pass void swap (int * a, int * B) {int temp; temp = * A; * A = * B; * B = temp; cout <* A <''<* B <'\ n';} int main () {int x = 1; int y = 2; swap (& X, & Y); cout <x <''<Y <'\ n';} // outputs 2 1, 2 1
In the previous program, the real parameters pass the addresses of X and Y. The form parameters define a and B as pointer variables, and the content of A and B is copy of X and Y addresses. *, * B is the content pointed to by the X and Y addresses, that is, X and Y.
Therefore, X and Y are also operated in the program. This program does not need to re-open new space in the memory and operate the real parameters between them.
Another transfer method is reference transfer, which has the same effect as pointer transfer.
// Reference transfer: void swap (Int & A, Int & B) {int temp; temp = A; A = B; B = temp; cout <A <<'' <B <'\ n';} int main () {int x = 1; int y = 2; swap (X, y); cout <x <''<Y <'\ n'; return 0 ;}
The previous code is the same as the pointer passing result, and both outputs are 2 1, 2 1
The function call method is the same as the value transfer method. However, the called function parameters reference the address of X and Y. The function operates on X and Y.
Pointer * And accessors in C &