如何在结构中打印变量?

时间:2021-03-01 20:46:40
#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;
}

In the main function, I want to display the result. How can I print variables defined in the structure? I tried %f, which returns 0.000000, and %d returns a random number.

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

4 个解决方案

#1


1  

You are doing mistake while printing,

你在打印时犯了错误,

Please note '&' is used to get the address of any variable, not the value.So when you are printing:

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

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

you are actually printing the adderss of variables:

你实际上是在打印变量的加法器:

&userInput.time (address of (userInput.time)), &userInput.weight(address of (userInput.weight)), &userInput.drag (address of (userInput.drag)).

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

You want to print their values, not address, hence remove '&' while printing:

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

ie;

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

#2


1  

int printf(const char *format, ...); is NOT int scanf(const char *format, ...);

int printf(const char * format,...);是非int scanf(const char * format,...);

scanf works with memory addresses of variables, while printf with variables itselves.

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

It's UB trying to printf ("%d", memory_addres_of_variable);

这是UB尝试printf(“%d”,memory_addres_of_variable);

because the right way to print memory addresses is with %zu starting from C99 and later, and %lu with ANSI C 90.

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

That's why you aree seeing a random number.

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

#3


1  

The issue is not related to struct. You are passing addresses of variables instead of their values to printf. So the solution is simple: Remove the & before variable names in the printf call.

该问题与struct无关。您将变量的地址而不是其值传递给printf。所以解决方案很简单:删除printf调用中的&之前的变量名称。

This is a common mistake: scanf needs the addresses of variables to be able to alter their values, while printf just takes the values. Unfortunately even the prototypes do not make it evident:

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

int printf(const char *format, ...);
int scanf(const char *format, ...);

And you need to use %f for double variables (derived from the type called float), %d is used for int (decimal).

并且您需要将%f用于双变量(从名为float的类型派生),%d用于int(十进制)。

The result:

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

References:

#4


0  

Well not sure about all the physics or which equation so I kind of faked that, but I got your program to compile and run so it should be pretty easy for you to fix it from here. Feel free to ask questions about the pointer stuff and why addresses were and weren't used in different places.

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

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

#1


1  

You are doing mistake while printing,

你在打印时犯了错误,

Please note '&' is used to get the address of any variable, not the value.So when you are printing:

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

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

you are actually printing the adderss of variables:

你实际上是在打印变量的加法器:

&userInput.time (address of (userInput.time)), &userInput.weight(address of (userInput.weight)), &userInput.drag (address of (userInput.drag)).

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

You want to print their values, not address, hence remove '&' while printing:

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

ie;

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

#2


1  

int printf(const char *format, ...); is NOT int scanf(const char *format, ...);

int printf(const char * format,...);是非int scanf(const char * format,...);

scanf works with memory addresses of variables, while printf with variables itselves.

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

It's UB trying to printf ("%d", memory_addres_of_variable);

这是UB尝试printf(“%d”,memory_addres_of_variable);

because the right way to print memory addresses is with %zu starting from C99 and later, and %lu with ANSI C 90.

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

That's why you aree seeing a random number.

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

#3


1  

The issue is not related to struct. You are passing addresses of variables instead of their values to printf. So the solution is simple: Remove the & before variable names in the printf call.

该问题与struct无关。您将变量的地址而不是其值传递给printf。所以解决方案很简单:删除printf调用中的&之前的变量名称。

This is a common mistake: scanf needs the addresses of variables to be able to alter their values, while printf just takes the values. Unfortunately even the prototypes do not make it evident:

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

int printf(const char *format, ...);
int scanf(const char *format, ...);

And you need to use %f for double variables (derived from the type called float), %d is used for int (decimal).

并且您需要将%f用于双变量(从名为float的类型派生),%d用于int(十进制)。

The result:

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

References:

#4


0  

Well not sure about all the physics or which equation so I kind of faked that, but I got your program to compile and run so it should be pretty easy for you to fix it from here. Feel free to ask questions about the pointer stuff and why addresses were and weren't used in different places.

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

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