Pointers are sometimes important 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 code above implements the square root of the output of a number
It would seem impossible to output the computed value in the main program after using a function with no return value of void.
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 parameter
Which means that the pointer in square points to the variable in the main function.
The value of num in the main function will also be changed in the function square where the pointer is worth changing
C + + (pointers)