码迷,mamicode.com
首页 > 其他好文 > 详细

<C Primer Plus 2 >Altering Variables in the Calling Function

时间:2017-03-19 17:11:59      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:ace   int   point   ptr   color   ati   and   cti   ble   

 1 #include <stdio.h>
 2 int diff(int x, int y);//the first form
 3 void interchange(int *u, int *v);//the second form
 4 int main(void){
 5     int x = 9;
 6     int y = 5;
 7     int z;
 8     printf("The Originally x = %d and y = %d\n", x, y);//alter variables in the calling function
 9     interchange(&x, &y);
10     z = diff(x, y);//need a value for some calculation or action
11     printf("Now  x = %d and y = %d\n", x, y);
12     printf("z = %d\n", z);
13     return 0;
14 }
15 
16 int diff(int x, int y){
17     return(x - y);
18 }
19 
20 void interchange(int *u,int *v){
21     int temp;
22 
23     temp = *u;
24     *u = *v;
25     *v = temp;
26 }

int function1(int x);

int function2(int *ptr);

Remeber:

1 Use the first form if the function needs a value for some calculation or action;

2 Use the second form if the function needs to alter variables in the calling function.

  One function does not have direct access to variables declared in another function. If you do need one function to acess another function‘s data,you can use pointer function arguments.

<C Primer Plus 2 >Altering Variables in the Calling Function

标签:ace   int   point   ptr   color   ati   and   cti   ble   

原文地址:http://www.cnblogs.com/michael2016/p/6580684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!