[英]How to print variables in a structure?
#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. 我尝试了
%f
,它返回0.000000,而%d
返回一个随机数。
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);
}
int printf(const char *format, ...);
is NOT int scanf(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. 这就是为什么您看到一个随机数。
The issue is not related to struct
. 这个问题与
struct
无关。 You are passing addresses of variables instead of their values to printf
. 您正在将变量的地址而不是它们的值传递给
printf
。 So the solution is simple: Remove the &
before variable names in the printf
call. 因此解决方案很简单:在
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. 这是一个常见的错误:
scanf
需要变量的地址才能更改其值,而printf
仅获取这些值。 Unfortunately even the prototypes do not make it evident: 不幸的是,即使是原型也没有表明这一点:
int printf(const char *format, ...);
int scanf(const char *format, ...);
And you need to use %f
for double
variables (derived from the type called f loat), %d
is used for int
( d ecimal). 而且您需要将
%f
用于double
变量(从称为f loat的类型派生), %d
用于int
( d十进制)。
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: 参考文献:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.