C帮助 - 具有两个值

时间:2015-11-20 15:37:13

标签: c

我有一个函数,输入变量由这个函数修改(我可以测试,最后的变量是正确的),函数在file1.c中,并在file1.h中声明,我由主文件(main.c)调用此函数。但是当在main.c中我使用这个函数时,变量不会被修改。

为了更容易理解,例如:

在file1.c - >

void funcion(char input1[], int input2){

--> Here some modificions happends

printf("Variables first are %c %d", input1[0], input2);
//the function end after this print

}

在file1.h->

void funcion(char input1[], int input2);

在main.c->

#include "file1.h"

main()
{
char *input1;
int input2;

function(input1, input2);
printf("Variables second are %c %d", input1[0], input2);
}

该计划的输出:

Variables first are A B

Variables second are C D

变量A = / = C和B = / = D.请有人帮忙吗?

1 个答案:

答案 0 :(得分:0)

您编写的函数具有这些变量的本地副本,因为您没有使用引用调用。您正在使用按值调用。因此,即使函数修改了变量,也不会对从main()传递的变量进行修改。它们是对范围仅限于该方法的变量的本地副本完成的。请阅读"按值调用"并且"通过引用和#34;进行调用。