C language value transfer and address transfer calls

Source: Internet
Author: User

The value transfer function does not change the values of two variables.

Why, because it is only necessary to change the value of a parameter in a function to call a value.

The value pointed to by the address changes not only within the function, but also outside the function.

See the Code:

 

1 # include <stdio. h>
2 # include <stdlib. h>
3
4 void swap1 (int x, int y)
5 {
6 int temp;
7
8 temp = X;
9 x = y;
10 y = temp;
11 printf ("output % d \ n", X, Y in the value passing function );
12}
13
14 void swap2 (int * X, int * Y)
15 {
16 int temp;
17 temp = * X;
18 * x = * Y;
19 * Y = temp;
20 printf ("output % d \ n in the address passing function", * X, * y );
21}
22
23 int main (INT argc, char * argv [])
24 {
25 int x = 10;
26 int y = 0;
27
28 printf ("x y \ n ");
29 printf ("Initial Value % d \ n", x, y );
30 // value passing subroutine call (SWAP xy)
31 swap1 (x, y );
32 printf ("calling % d \ n", x, y outside the value passing function );
33
34 // call the address character Transfer Program (SWAP x, y)
35 swap2 (& X, & Y );
36 printf ("calling outside the Address Transfer Function % d \ n", x, y );
37 system ("pause ");
38 return 0;
39}
40

 

 

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.