How to change the value of the main function variable when arguments and parameter pointers do function arguments

Source: Internet
Author: User

Outputs two integers for input in size order

The code is as follows:

#include <iostream>using namespace Std;int main () {    void swap (int *p1,int *p2);    int *pointer_1,*pointer_2,a,b;    cin>>a>>b;    pointer_1=&a;    pointer_2=&b;    if (a<b) swap (pointer_1,pointer_2);    cout<< "max=" <<a<< "<<" min= "<<b<<endl;    return 0;} <span style= "color: #ff0000;" >void Swap (int *p1,int *p2) {    int temp;    TEMP=*P1;    *P1=*P2;    *p2=temp;} </span>
Note: Use thevariablesWhen a function parameter is used, the combination of the real and the false is a one-way ' value pass ' that only passes the data from the parameter to the parameter, and the parameter value cannot be returned to the argument, that is, the value of the argument does not change because of the change of the parameter. (This rule should also be followed when a pointer variable makes a function argument, except when the character array name is a function parameter)What I want to say here is that when you do a function argument with a pointer variable, the value of the variable pointed to by the pointer changes during the execution of the function, and after the function call ends, changes to the values of these variables are preserved. This makes it possible to change the value of a variable by calling a function, and the changed values are used in the keynote function.

However, you cannot attempt to change the value of an argument pointer variable by changing the value of the parameter pointer variable. For example, this:

void swap (int *p1,int *p2) {    int *temp;    TEMP=P1;    P1=P2;    P2=temp;}
So calling a function does not change the value of the argument pointer variable, but it can change the value of the variable that the argument pointer variable is pointing to.

Copyright NOTICE: This article is my original article, we can reprint Oh!!

How to change the value of the main function variable when arguments and parameter pointers do function arguments

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.