C 未初始化变量
我试着写两个函数,一个接受输入,另一个计算加速度。编译器告诉我,我的变量未初始化,但它们应该具有来自输入的值。我做错了什么C 未初始化变量,c,function,variables,C,Function,Variables,我试着写两个函数,一个接受输入,另一个计算加速度。编译器告诉我,我的变量未初始化,但它们应该具有来自输入的值。我做错了什么 #include <stdio.h> #include <stdlib.h> void input_instructions(double vi, double vf); double compute_acceleration(double vi, double vf); int main() { double vi; doubl
#include <stdio.h>
#include <stdlib.h>
void input_instructions(double vi, double vf);
double compute_acceleration(double vi, double vf);
int main()
{
double vi;
double vf;
double acc;
double t;
input_instructions(vi, vf);
acc = compute_acceleration(vi,vf);
t = (vf - vi)/acc;
printf("The constant acceleration of the cyclist is %.2f and it will take him %.2f minutes/seconds/"
"to come to rest with an initial velocity of 10mi/hr.\n", acc, t);
}
void input_instructions(double vi, double vf)
{
printf("This program will calculate the rate of accleration and the time it takes/"
"the cyclist to come to rest\n");
printf("Enter inital velocity=>");
scanf("%lf", &vi);
printf("Enter final velocity");
scanf("%lf", &vf);
}
double compute_acceleration(double vi, double vf)
{
double t = 1;
double a = (vf-vi)/t;
return (a);
}
#包括
#包括
无效输入_指令(双vi、双vf);
双计算加速度(双vi,双vf);
int main()
{
双vi;
双心室颤动;
双acc;
双t;
输入_指令(vi、vf);
acc=计算加速度(vi,vf);
t=(vf-vi)/acc;
printf(“骑车人的恒定加速度为%.2f,需要%.2f分/秒/”
“以10mi/hr的初始速度静止。\n”,acc,t);
}
无效输入_指令(双vi、双vf)
{
printf(“此程序将计算加速率和所需时间/”
“骑自行车来休息的人\n”);
printf(“输入初始速度=>”;
scanf(“%lf”、&vi);
printf(“输入最终速度”);
扫描频率(“%lf”、&vf);
}
双计算加速(双vi,双vf)
{
双t=1;
双a=(vf vi)/t;
报税表(a);
}
哇,这里发生了很多坏事。除了main()中声明的未初始化变量(是的,编译器是正确的),C按值传递参数。vi和vf参数值存储在堆栈上。然后,您的scanf获取堆栈变量的地址,为其赋值,函数返回,赋值消失-poof
void input_instructions(double vi, double vf)
{
printf("Enter inital velocity=>");
scanf("%lf", &vi);
printf("Enter final velocity");
scanf("%lf", &vf);
// function returns and input parameter values are gone.
}
您希望传入指向变量的指针,如下所示:
void input_instructions(double *vi, double *vf)
{
printf("This program will calculate the rate of accleration and the time it takes/"
"the cyclist to come to rest\n");
printf("Enter inital velocity=>");
scanf("%lf", vi);
printf("Enter final velocity");
scanf("%lf", vf);
input_instructions(&vi, &vf);
并从main()调用如下所示:
void input_instructions(double *vi, double *vf)
{
printf("This program will calculate the rate of accleration and the time it takes/"
"the cyclist to come to rest\n");
printf("Enter inital velocity=>");
scanf("%lf", vi);
printf("Enter final velocity");
scanf("%lf", vf);
input_instructions(&vi, &vf);
请参阅。将vi、vf、acc和t声明为全局变量(主变量之外)
#包括
#包括
无效输入_指令();
双重计算加速度();
双vi;
双心室颤动;
双acc;
双t;
int main()
{
输入_指令();
acc=计算加速度();
t=(vf-vi)/acc;
printf(“骑车人的恒定加速度为%.2f,需要%.2f分/秒/”
“以10mi/hr的初始速度静止。\n”,acc,t);
}
无效输入_指令()
{
printf(“此程序将计算加速率和所需时间/”
“骑自行车来休息的人\n”);
printf(“输入初始速度=>”;
scanf(“%lf”、&vi);
printf(“输入最终速度”);
扫描频率(“%lf”、&vf);
}
双计算加速度()
{
双t=1;
双a=(vf vi)/t;
报税表(a);
}
或者您可以使用指针并将其传递给“input_instructions”函数 您正在输入指令函数中读取变量,因此无法“传入”。由于它们的内存在函数内部旋转,因此在函数外部无法访问它们 您的一个选择是,通过在main之外声明变量vi和vf,将它们更改为全局变量,不必麻烦将它们放在input_函数的签名中,其余的就可以了
#include <stdio.h>
#include <stdlib.h>
/* Prototypes */
void input_instructions();
double compute_acceleration(double new_vi, double new_vf);
/* Globals */
double vi;
double vf;
int main()
{
我已经更改了变量的名称,以帮助理解变量的内存位置在哪里,并说明在您的示例中,main中的vi实际上与调用方法中的vi不同 In
void input_instructions(double vi, double vf)
{
.
.
}
变量vi
是vf
是函数的局部变量,与main()
函数中类似命名的变量无关。换句话说,它们具有函数作用域
,并且在函数返回后不再存在
你应该做的是:
input_instructions(&vi,&vf); // pass the address in the main()
并将函数重写为:
void input_instructions(double *vi, double *vf) // Using pointers
{
printf("This program will calculate the rate of accleration and the time it takes/"
"the cyclist to come to rest\n");
printf("Enter inital velocity=>");
scanf("%lf", vi); // Hmm, you're changing the value in the main()
printf("Enter final velocity");
scanf("%lf", vf); // Note no '&' since we're dealing with pointers.
}
这里使用函数
的形式参数*vf
和*vi
对main()
的vf
和*vi
进行了持久的更改,这不是C语言中函数的工作方式。也许你会觉得有点不知所措。你在C班的哪一部分?你知道什么是指针和按引用传递吗?我不知道。我们还没有学习过pass-by和指针。需要修正什么?C是严格意义上的pass-by值!它不会让我这样做的。我不知道为什么会被否决。我怀疑全局变量解决方案就是教授在本例中寻找的解决方案,因为他指出它们还没有涵盖通过引用传递。我如何使两个函数的变量在主函数中彼此局部?函数旋转它们自己的变量,只有它们可以在堆栈上使用。它们对任何其他函数都不可用,一旦函数结束,它们就会从堆栈中清除。main调用的函数甚至不能从main访问变量。我给出的第一个解决方案使用全局变量,而不是局部变量。全局变量是在堆上而不是在堆栈上创建的,因此它们可以在整个解决方案中访问。第二个解决方案使用pass-by-reference。新变量仍然在main调用的函数中旋转,但它们(通过参数)将main中的值的堆栈上的内存位置传递给它们。这使您能够进行影响堆栈中通常无法访问的部分的更改。