Pointers sometimes play an important role in programming
We can use it to do some 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 implements the square root of the output number
It would have been impossible to output the computed value in the main program after using void, a function that did not return a value.
But after using the pointer, you can easily implement the
In the main program, we define a num=2
Use Square (&num) to pass the address of a variable to the function pointer argument
Which means that the pointer in square points to a variable in the main function.
The pointer in function square is worth changing and will change the value of num in the main function
This article on the C + + pointer (must see) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.