A brief talk on C + + pointer (must-see) _c language

Source: Internet
Author: User

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.

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.