如何在结构中打印变量? #包括 #包括 #定义G 9.81 类型定义结构 { 双倍重量; 双阻力; 双倍时间; }用户输入; 双重计算水平(用户输入); int main(int argc,字符**argv) { 用户输入用户输入; 双速; printf(“请输入重量、阻力和时间:”); scanf(“%lf%lf%lf”、&userInput.weight、&userInput.drag、&userInput.time); 速度=计算速度(用户输入); printf(“在t=%f时,重量为%d kg\n且阻力系数为%8.2f kg/s\n的降落伞手的速度为%8.2f m/s^2”,&userInput.time,&userInput.weight,&userInput.drag,velocity); 返回0; } 双重计算级别(用户输入数据) { 双速; //TODO计算速度 返回速度; }

如何在结构中打印变量? #包括 #包括 #定义G 9.81 类型定义结构 { 双倍重量; 双阻力; 双倍时间; }用户输入; 双重计算水平(用户输入); int main(int argc,字符**argv) { 用户输入用户输入; 双速; printf(“请输入重量、阻力和时间:”); scanf(“%lf%lf%lf”、&userInput.weight、&userInput.drag、&userInput.time); 速度=计算速度(用户输入); printf(“在t=%f时,重量为%d kg\n且阻力系数为%8.2f kg/s\n的降落伞手的速度为%8.2f m/s^2”,&userInput.time,&userInput.weight,&userInput.drag,velocity); 返回0; } 双重计算级别(用户输入数据) { 双速; //TODO计算速度 返回速度; },c,struct,printf,C,Struct,Printf,在main函数中,我想显示结果。 如何打印结构中定义的变量? 我尝试了%f,它返回0.000000,%d返回一个随机数。我不确定所有的物理或哪个方程,所以我有点伪造了,但我让你的程序编译并运行,所以你应该很容易从这里修复它。请随意询问有关指针的问题,以及为什么在不同的地方使用和不使用地址 #include <stdio.h> #include <math.h> #define G 9.81 typedef struct { double weight;

在main函数中,我想显示结果。 如何打印结构中定义的变量?
我尝试了
%f
,它返回0.000000,
%d
返回一个随机数。

我不确定所有的物理或哪个方程,所以我有点伪造了,但我让你的程序编译并运行,所以你应该很容易从这里修复它。请随意询问有关指针的问题,以及为什么在不同的地方使用和不使用地址

#include <stdio.h>
#include <math.h>
#define G 9.81

typedef struct
{
    double weight;
    double drag;
    double time;
} USER_INPUT;

double calculateVelocity(USER_INPUT);

int main(int argc, char **argv)
{
    USER_INPUT userInput;
    double velocity;

    printf("Please enter weight, drag and time: ");
    scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time);

    velocity = calculateVelocity(userInput);

    printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity);

    return 0;
}

double calculateVelocity(USER_INPUT data)
{
    double velocity;
    // TODO compute velocity
    return velocity;
}
#包括
#包括
#定义G 9.81
#定义P 1.00/?
#定义一个1.00/?
类型定义结构{
双倍重量;
双阻力;
双倍时间;
}用户输入;
双重计算级别(用户输入*);//终端速度
双重计算级别(用户输入数据){
返回sqrt((2*数据->重量*G)/(P*A*数据->拖动));
}
内部主(空){
用户输入\u t用户输入;
printf(“请输入重量、阻力和时间:”);
scanf(“%lf%lf%lf”、&userInput.weight、&userInput.drag、&userInput.time);
双速度=计算速度(&用户输入);
printf(“\nAt t=%f,重量为%f kg的降落伞手\n”
“和阻力系数%f kg/s\n”
“将具有%f m/s^2的速度\n”,
userInput.time、userInput.weight、userInput.drag、velocity);
}
int printf(常量字符*格式,…)不是
int-scanf(常量字符*格式,…)

scanf
使用变量的内存地址,而printf使用变量本身

它正试图
printf(“%d”,变量的内存地址)

因为打印内存地址的正确方法是使用从C99及更高版本开始的
%zu
,以及使用ANSI C 90的
%lu


这就是你看到随机数的原因。

这个问题与
struct
无关。您正在将变量的地址而不是值传递给
printf
。因此,解决方案很简单:在
printf
调用中删除变量名之前的
&

这是一个常见的错误:
scanf
需要变量的地址才能改变它们的值,而
printf
只接受值。不幸的是,即使是原型也不清楚:

#include <stdio.h>
#include <math.h>

#define G 9.81
#define P 1.00 // ?
#define A 1.00 // ?

typedef struct {
    double weight;
    double drag;
    double time;
} userInput_t;

double calculateVelocity(userInput_t *); // Terminal velocity?

double calculateVelocity(userInput_t *data) {
   return sqrt((2 * data->weight * G)/(P * A * data->drag));
}

int main(void) {

    userInput_t userInput;

    printf("Please enter weight, drag and time: ");
    scanf("%lf %lf %lf", &userInput.weight, &userInput.drag, &userInput.time);


    double velocity = calculateVelocity(&userInput);

    printf("\nAt t = %f, the parachutist with weight %f kg\n"
           "and a drag coefficient %f kg/s\n"
           "will have a velocity of %f m/s^2\n", 
              userInput.time, userInput.weight, userInput.drag, velocity);
}
对于
double
变量(从名为float的类型派生),需要使用
%f
%d
用于
int
decimal)

结果是:

int printf(const char *format, ...);
int scanf(const char *format, ...);
参考资料:


您在打印时出错

请注意“&”用于获取任何变量的地址,而不是值。因此,在打印时:

printf("At t = %f , the parachutist with weight %f kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity);
实际上,您正在打印变量的加法:

&userInput.time(userInput.time)的地址, &userInput.weight(userInput.weight)的地址,&userInput.drag(userInput.drag)的地址

您希望打印它们的值,而不是地址,因此在打印时删除“&”:

ie


从代码中,我看到可变速度没有以任何方式计算或分配。使用%f打印时,您将获得0.000;使用%d打印时,您将获得一个垃圾值。你能分享你计算速度的公式吗?使用
userInput.time,userInput.weight。。。等等。
也许?注意,
calculateVelocity
返回一个不确定的值,从中读取是未定义的行为。“因为打印内存地址的正确方法是从C99及更高版本开始使用%zu,从ANSI C 90开始使用%lu。”否
%zu
用于打印
大小t
lu
用于
无符号长字符
。两者都与内存地址无关。对于
void*
使用
%p
,或者对于
uintptpr\t
使用
“%”优先级“u”
printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", &userInput.time, &userInput.weight, &userInput.drag, velocity);
printf("At t = %f , the parachutist with weight %d kg\nand a drag coefficient %8.2f kg/s \n will have a velocity of %8.2f m/s^2", userInput.time, userInput.weight, userInput.drag, velocity);