<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.

时间: 2024-10-12 05:17:05

<C Primer Plus 2 >Altering Variables in the Calling Function的相关文章

The annotation of &lt;&lt;C++ primer&gt;&gt; {藤原豆腐坊自家用}

The annotation of <<C++ primer>> {藤原豆腐坊自家用} 给变量名一个初始值几乎总是正确的. 但不要求必须这么做 C++的主要设计目的之一就是允许程序员自定义类型,而这些类型和内置类型一样易于使用. 什么是对象? 一般而言, 对象是内存中具有类型的区域,说的具体一些, 计算左值表达式就会产生对象. 关于初始化 C++支持两种初始化方式, 复制初始化 copy initialization 直接初始化 direct-initialization int

(翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope

总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environment和environment record(被作者合并称为 binding objects)的过程.非常值得一看     个人翻译: How variables are allocated memory in Javascript? It's actually a very interesting

[C++] Fucntions

Statements A break statements terminate the nearest wile, do while, for or switch statement. A break affect only the nearest enclosing loop or switch. As any block, variables declared inside a try block are inaccessible outsite the block - in particu

System and method for dynamically adjusting to CPU performance changes

FIELD OF THE INVENTION The present invention is related to computing systems, and more particularly to a system and method for adjusting to changes in processor performance. BACKGROUND INFORMATION Designers of mobile computing platforms are faced wit

Application binary interface and method of interfacing binary application program to digital computer

An application binary interface includes linkage structures for interfacing a binary application program to a digital computer. A function in a relocatable shared object module obtains the absolute address of a Global Offset Table (GOT) in the module

UNREAL ENGINE 4.12 正式发布!下载地址

UNREAL ENGINE 4.12 正式发布! 下载地址:https://www.unrealengine.com/ Alexander Paschall 在 June 1, 2016 |功能新闻社区 Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn 此版本内含虚幻引擎 4 的数百个更新,以及 GitHub 虚幻引擎开发者社区提交的 106 项改良!特此对虚幻引擎 4.12 版本的贡献者们表达诚挚谢意:

ionic v4修改组件样式

新项目使用Ionic 4, Ionic 4的组件使用了shadow DOM和CSS4变量. 我们需要注意API文档中的 CSS Custom Properties 假如我们想要修改ion-item组件的背景颜色. 我们可以在home.page.scss中这样写: ion-item { --ion-item-background-color-active: #000; --min-height: 100px; } 在 theme/variables.scss中 使用 :root 选择器进行全局修改

傻瓜学习JavaScript闭包(译)

在<高级程序设计>中,对于闭包一直没有很好的解释,在stackoverflow上翻出了一篇很老的<JavaScript closure for dummies>(2016)~ 出处:http://stackoverflow.com/questions/111102/how-do-javascript-closures-work 闭包不是魔法 本文旨在用JavaScript代码让程序员理解闭包,函数式编程的程序员或者导师请绕行. 只要理解了闭包的核心理念,闭包并不难学.但是通过学习一

第4章:缓冲区、着色器、GLSL

原文链接: http://www.rastertek.com/gl40tut04.html Tutorial 4: Buffers, Shaders, and GLSL This tutorial will be the introduction to writing vertex and pixel shaders in OpenGL 4.0. It will also be the introduction to using vertex and index buffers in OpenG