I. pointer and address description:
1. The address operator can only be applied to objects in memory, that is, variables and array elements. It cannot act on expressions, constants, or register-type variables.
2. The unary operator * is an indirect addressing or indirect reference operator. When used as a pointer, the object to which the access Pointer Points
3. pointers can only target certain types of objects. One exception is that a pointer to the void type can store a pointer to any type, but it cannot indirectly reference itself.
Ii. pointers and functions
C language passes the parameter value to the called function by passing the value. Therefore, the called function cannot directly modify the variable value in the main function.
However, you can use pointers to indirectly access the operations they direct.
[Html]
# Include <stdio. h>
Void swap (int * x, int * y );
Int main ()
{
Int x = 1;
Int y = 2;
Swap (& x, & y );
Printf ("x = % d y = % d \ n", x, y );
}
Void swap (int * x, int * y)
{
Int temp;
Temp = * x;
* X = * y;
* Y = temp;
}
The output is as follows:
[Html]
Pateo @ pateo-B86N53X :~ /Work/study $ cc main. c-o main
Pateo @ pateo-B86N53X :~ /Work/study $./main
X = 2 y = 1
For ease of understanding, I modified the following example, learning is improving in the attempt, OK?
[Html]
# Include <stdio. h>
Void swap (int * x, int * y );
Int main ()
{
Int m = 1;
Int n = 2;
Swap (& m, & n );
Printf ("m = % d n = % d \ n", m, n );
}
Void swap (int * x, int * y)
{
Int temp;
Temp = * x;
Printf ("temp = % d \ n", temp );
* X = * y;
* Y = temp;
}
Output:
[Html]
Pateo @ pateo-B86N53X :~ /Work/study $ cc main. c-o main
Pateo @ pateo-B86N53X :~ /Work/study $./main
Temp = 1
M = 2 n = 1
At this point, I have realized that the pointer parameters of the function are different from those of the java function parameters. Below I will analyze the above Code as follows:
[Html]
Int * x;
Int * y;
X = & m;
Y = & n
Transport substitution in Functions
Temp = * (& m)
* (& M) = * (& n)
* (& N) = temp
It should be better understood from above