C ++ (pointer), pointer
Pointers are sometimes important in programming.
We can use it to complete seemingly impossible tasks.
# Include <iostream>
Using namespace std;
Void square (int * n ){
* N = * n ** n;
}
Int main (){
Int num = 2;
Cout <"The original number is" <num <endl;
Square (& num );
Cout <"The new value of number is" <num <endl;
Return 0;
}
The above code outputs the square root of a number.
It seems impossible to output computed values in the main program after using void, a function without return values.
However, the pointer can be easily implemented.
In the main program, we define a num = 2
Use square (& num) to pass the address of a variable to the function pointer parameter.
That is to say, the pointer in square points to the variable in the main function.
Changing the pointer value in the square function also changes the num value in the main function.