C + + (pointers)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.