我的结构有什么问题?
好吧,我这里用c写。在 mingw gcc 中编译。
我正在尝试做一些非常简单的事情。创建一个包含 3 个浮点数 x、y、z 的向量结构。
然后我希望能够和他们一起做一些数学题。
这是我的简短测试程序:
#ifndef _PHYSICS_C_
#define _PHYSICS_C_
#define SUCCESS 0
#define FAILURE 1
typedef struct {
float x;
float y;
float z;
}vector;
int add ( vector* a, vector* b, vector* destination ){
(*destination).x = (float)( ((*a).x) + ((*b).x) );
(*destination).y = (float)( ((*a).y) + ((*b).y) );
(*destination).z = (float)( ((*a).z) + ((*b).z) );
return SUCCESS;
}
int main(int argc, char** argv){
printf("creating vectors\n\n");
vector a = {1.0f,5.0f,3.0f};
vector b = {2.0f,3.0f,6.0f};
vector destination;
printf("adding vectors\n\n");
if(add(&a, &b, &destination) == SUCCESS){
printf("result: (%d, %d, %d)\n\n",destination.x,destination.y,destination.z);
} else {
printf("the program failed somehow...\n\n");
}
printf("Press any key to continue...\n");
getchar();
return SUCCESS;
}
#endif
当我编译并运行它时,它应该返回 (3, 8, 9) 向量 a 和 b 的总和。
相反,它返回 (0, 1074266112, 0)...
我不知道出了什么问题。
出于某种原因,我认为我一定是在以某种方式重写了我不应该这样做的记忆。
Ok, i'm writing in c here. Compiling in mingw gcc.
I'm trying to do something really simple. create a vector struct containing 3 floats x,y,z.
then I want to be able to do some math with them.
This is my short test program:
#ifndef _PHYSICS_C_
#define _PHYSICS_C_
#define SUCCESS 0
#define FAILURE 1
typedef struct {
float x;
float y;
float z;
}vector;
int add ( vector* a, vector* b, vector* destination ){
(*destination).x = (float)( ((*a).x) + ((*b).x) );
(*destination).y = (float)( ((*a).y) + ((*b).y) );
(*destination).z = (float)( ((*a).z) + ((*b).z) );
return SUCCESS;
}
int main(int argc, char** argv){
printf("creating vectors\n\n");
vector a = {1.0f,5.0f,3.0f};
vector b = {2.0f,3.0f,6.0f};
vector destination;
printf("adding vectors\n\n");
if(add(&a, &b, &destination) == SUCCESS){
printf("result: (%d, %d, %d)\n\n",destination.x,destination.y,destination.z);
} else {
printf("the program failed somehow...\n\n");
}
printf("Press any key to continue...\n");
getchar();
return SUCCESS;
}
#endif
When I compile and run it, it should return (3, 8, 9) the sum of vectors a and b.
instead it returns (0, 1074266112, 0)...
I can't figure out what is wrong.
for some reason I think that I must somehow be writing over memory I'm not supposed to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
x,y,z 是浮点数,但您试图将它们打印为整数。
尝试:
检查 man printf 或您的文档以查看 printf 的所有说明符。
x,y,z are floats but you are trying to print them as integers.
try:
check
man printf
or your documentation to see all of the specifiers for printf.%d
需要int
。使用%f
或%g
表示浮点数/双精度数。%d
expectsint
. Use%f
or%g
for floats/doubles.以下划线开头且后跟大写字母的标识符被保留;不要在您自己的代码中使用它们。
包含防护 (
#ifndef _PHYSICS_C_ ...
) 适用于头文件,不适用于.c
文件。printf
需要#include
。您将从
main()
返回值SUCCESS
。没关系,因为SUCCESS
恰好为 0,但使用EXIT_SUCCESS
(在
中声明)会更清楚,或者只是return 0;
。您的
add
函数总是返回SUCCESS
。它也可能是一个void函数(并且对其值进行测试)。main
没有用)。除非您预计稍后会添加错误检查,否则表达式已经是
float
类型了。而(*foo).bar
最好写成foo->bar
例如,第一个赋值可以简化为destination->。 ;x = a->x + y->x;
真正的问题(已经指出)是您对
float< 类型的值使用
"%d"
格式。 通常使用double而不是
float
它具有更高的精度,并且现代硬件通常针对双精度运算进行了优化。如果您在编译器中启用警告,它可能会告诉您其中一些问题。
Identifiers starting with an underscore followed by an uppercase letter are reserved; don't use them in your own code.
Include guards (
#ifndef _PHYSICS_C_ ...
) are for header files, not for.c
files.printf
requires#include <stdio.h>
.You're returning the value
SUCCESS
frommain()
. That's ok, sinceSUCCESS
happens to be 0, but it would be clearer to use eitherEXIT_SUCCESS
(declared in<stdlib.h>
or justreturn 0;
.You
add
function always returnsSUCCESS
. It might as well be a void function (and the test for its value inmain
is not useful). Unless you anticipate adding error checking later on.The casts in your
add
function are unnecessary; the expression is already of typefloat
. And(*foo).bar
is better written asfoo->bar
. For example, the first assignment can be simplified todestination->x = a->x + y->x;
.The real problem (which has already been pointed out) is that you're using a
"%d"
format for values of typefloat
.It's usual to use
double
rather thanfloat
. It has more precision, and modern hardware is often optimized for double-precision operations.If you enable warnings in your compiler, it will probably tell you about some of these problems.
您正在使用格式说明符
%d
打印float
,该格式说明符适用于signed int
。请改用%f
或%g
或%e
。另外,为什么不这样做呢:
这样对眼睛来说更容易。 (虽然不是问题)。
You are printing
float
with format specifier%d
which is intended forsigned int
. Use%f
or%g
or%e
instead .Also, why don't you do:
Its much easier on the eyes. (although not a problem).