C中的更新(int)变量[复制]

时间:2022-09-06 10:19:02

This question already has an answer here:

这个问题在这里已有答案:

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?

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

5 个解决方案

#1


13  

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

stage通过value传递给changeStage。 stage = 1只更改changeStage中stage的本地值,而不是main中stage的值。你必须传递一个指针:

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

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

#2


7  

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'值的位置。然后,该函数可以修改该位置中的内容。

#3


3  

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

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

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);
    }
}

&stage says to take the address of stage and pass that to the function. The stage_p argument will then point to the int in the main.

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

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

* stage_p使它使用stage_p指向的值,这是你案例中主要的阶段。

Further reading

#4


3  

You are not modifying the original stage variable, but only modifying an local copy inside changeStage function.

您没有修改原始阶段变量,只修改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语言中非常重要的特性。

#5


2  

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();
 }
}

#1


13  

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

stage通过value传递给changeStage。 stage = 1只更改changeStage中stage的本地值,而不是main中stage的值。你必须传递一个指针:

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

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

#2


7  

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'值的位置。然后,该函数可以修改该位置中的内容。

#3


3  

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

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

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);
    }
}

&stage says to take the address of stage and pass that to the function. The stage_p argument will then point to the int in the main.

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

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

* stage_p使它使用stage_p指向的值,这是你案例中主要的阶段。

Further reading

#4


3  

You are not modifying the original stage variable, but only modifying an local copy inside changeStage function.

您没有修改原始阶段变量,只修改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语言中非常重要的特性。

#5


2  

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();
 }
}