首页 > 代码库 > <C Primer Plus 2 >Altering Variables in the Calling Function
<C Primer Plus 2 >Altering Variables in the Calling Function
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。