C Language: Pass the value, pass the address

Source: Internet
Author: User

Formal parameter: Formal parameters
Arguments: Actual parameters

Pass Value:
copy The value of the argument to the parameter,
Modifying a parameter within a function does not affect the argument.

Transfer address:
Pointer pass-through value, parameter is a pointer to the address of the actual parameter
When you point to a parameter, the action is equivalent to the argument itself

#include <iostream>//Swap Location//"Value Call"voidSwap_value (intXinty) {inttemp =x; X=y; Y=temp;}//"Reference call 1", formal parameter: Pass the address, get the value (*)voidSwap_p (int*x,int*y) {inttemp = *x; *x = *y; *y =temp;}//"Reference Call 2", formal parameter: Pass the value, followed by the addressvoidSWAP_ADDR (int&a,int&b) {inttemp =A; A=b; b=temp;} Main () {intx =0; inty =1; printf ("x =%d, y =%d\n", x, y);    Swap_value (x, y); printf ("after a value call: x =%d, y =%d\n", x, y); Swap_p (&x, &y); printf ("after the address call: x =%d, y =%d\n", x, y);    Swap_addr (x, y); printf ("Pass-through address is called after: x =%d, y =%d\n", x, y);}
x = 0, y = 1 after the call: x = 0, y = 1 after the address call: x = 1, y = 0 value after address call: x = 0, y = 1

C Language: Pass the value, pass the address

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.