C语言 在函数内更新 C 中的 (int) 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23667497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:05:17  来源:igfitidea点击:

Update (int) variable in C inside a function

c

提问by user3589718

I'm trying to write a function that changes screen in my simple C snake game.

我正在尝试编写一个可以在我的简单 C 蛇游戏中更改屏幕的函数。

main(){
  int stage = 0;
  ...
  ..
  .
  while(stage!=0){
    //if snake hits wall
    changeStage(stage);
  }
}

function:

功能:

void changeStage(int stage){
  stage = 1;
}

This code does not update the code, it will keep on running. What is wrong with my code?

此代码不会更新代码,它将继续运行。我的代码有什么问题?

回答by univerio

stageis passed by value to changeStage. stage = 1only changes the local value of stagein changeStage, not the value of stagein main. You have to pass a pointer instead:

stage按值传递给changeStage. stage = 1只改变局部值stagechangeStage,而不是价值stagemain。你必须传递一个指针:

while (stage != 0) {
    changeStage(&stage);
}

void changeStage(int *stage) {
    *stage = 1;
}

回答by theorifice

C is a pass by value language. The simplest approach to accomplish what you want is to pass a reference to the variable you need to change. For example:

C 是一种传值语言。完成您想要的最简单的方法是传递对您需要更改的变量的引用。例如:

void changeStage(int *stage){
  *stage = 1;
}

main(){
  int score = 0;
  int stage = 0;
  ...
  ..
  .
  while(stage!=0){
    //if snake hits wall
    changeStage(&stage);
 }
}

Note: You may need to read up on pointers to fully understand the code if you are just beginning with C programming. In the example code, instead of passing the value of 'stage', you pass the location where the value of 'stage' is stored. The function can then modify the contents in the location.

注意:如果您刚开始使用 C 编程,您可能需要阅读指针以完全理解代码。在示例代码中,不是传递 'stage' 的值,而是传递存储 'stage' 值的位置。然后该函数可以修改该位置中的内容。

回答by jiandingzhe

You are not modifying the original stagevariable, but only modifying an local copyinside changeStagefunction.

您不是在修改原始stage变量,而只是修改了函数内部的本地副本changeStage

You need to use a pointer:

您需要使用指针:

void changeStage(int* stage)
{
  *stage = 1;
}

using the function:

使用函数:

while (stage != 0)
{
    // if snake hits wall
    changeStage(&stage);
}

You need learn more basic concepts of C language. Pointer is an very important feature in C language.

您需要了解更多 C 语言的基本概念。指针是C语言中一个非常重要的特性。

回答by Ryan Haining

C function arguments are pass-by-value. This means that instead of passing a referenceto stageyou are passing the value stored in it. The update you do the in changeStagefunction then only applies to the copy that has been made.

C 函数参数是按值传递的。这意味着不是将引用传递给stage您,而是传递存储在其中的值。您执行 inchangeStage函数的更新仅适用于已制作的副本。

If you want to update a variable in another function, you will need to pass a pointer to it.

如果你想更新另一个函数中的变量,你需要传递一个指向它的指针。

void changeStage(int* stage_p){
    *stage_p = 1;
}

int main() {
    //...
    while(stage!=0){
        //if snake hits wall
        changeStage(&stage);
    }
}

&stagesays to take the address of stageand pass that to the function. The stage_pargument will then point to the int in the main.

&stage说要获取的地址stage并将其传递给函数。然后stage_p参数将指向 main 中的 int。

*stage_pcauses it to use the value pointed to by stage_p, which is stagein the main in your case.

*stage_p导致它使用指向的值stage_p,这stage在您的情况下是主要的。

Further reading

进一步阅读

回答by Survaf93

Correct, you need to pass the pointer if you wish to change the value of the variable in main() or you can create it as global variable, that way it's accesable both in functions and in main.

正确,如果您希望更改 main() 中变量的值,则需要传递指针,或者您可以将其创建为全局变量,这样它在函数和 main 中都可以访问。

static int stage = 0;

void changeStage(){
  stage = 1;
  }

main(){
int score = 0;

 ...
 ..
 .
 while(stage!=0){
   //if snake hits wall
   changeStage();
 }
}