C ++ (pointer), pointer

Source: Internet
Author: User

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.

 

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.