【发布时间】:2016-02-16 13:40:48
【问题描述】:
我遇到了这个问题:
#include <stdio.h>
void change_number(int *x);
int main()
{
int x;
printf("Enter the number x: ")
scanf("%d", &x);
printf("In the main program: x = %d\n", x);
change_number(&x);
printf("In the main program: x = %d\n", x);
return 0;
}
void change_number(int *x)
{
x = *x+3;
printf("In the subroutine: x = %d\n", x);
}
预期输出:
Enter the number x: 555
In the main program: x = 555
In the subroutine: x = 558
In the main program: x = 558
两个音符:
我无法修改 main 或之前的任何内容。我只能修改
void change_number函数,里面的代码是我自己的代码。我无法获得所需的输出——我的代码只在子程序中输出
x+3,但在主程序中没有改变它。
我将如何更改主程序中的值?请记住,我对 C 很陌生,还不太了解(事实上,我昨天刚刚介绍了指针)。
【问题讨论】:
-
您需要使用标准的 C 编译器编译代码。例如,如果您有 GCC 编译器,则可以使用选项
-std=c11 -pedantic-errors将其转换为标准 C 编译器。