我的结构有什么问题?

发布于 2024-12-02 21:47:00 字数 1326 浏览 0 评论 0原文

好吧,我这里用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没有你 2024-12-09 21:47:00

x,y,z 是浮点数,但您试图将它们打印为整数。

尝试:

        printf("result: (%f, %f, %f)\n\n",destination.x,destination.y,destination.z);

检查 man printf 或您的文档以查看 printf 的所有说明符。

x,y,z are floats but you are trying to print them as integers.

try:

        printf("result: (%f, %f, %f)\n\n",destination.x,destination.y,destination.z);

check man printf or your documentation to see all of the specifiers for printf.

勿忘初心 2024-12-09 21:47:00

%d 需要 int。使用 %f%g 表示浮点数/双精度数。

%d expects int. Use %f or %g for floats/doubles.

聆听风音 2024-12-09 21:47:00

以下划线开头且后跟大写字母的标识符被保留;不要在您自己的代码中使用它们。

包含防护 (#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 from main(). That's ok, since SUCCESS happens to be 0, but it would be clearer to use either EXIT_SUCCESS (declared in <stdlib.h> or just return 0;.

You add function always returns SUCCESS. It might as well be a void function (and the test for its value in main is not useful). Unless you anticipate adding error checking later on.

The casts in your add function are unnecessary; the expression is already of type float. And (*foo).bar is better written as foo->bar. For example, the first assignment can be simplified to destination->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 type float.

It's usual to use double rather than float. 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.

星星的軌跡 2024-12-09 21:47:00

您正在使用格式说明符 %d 打印 float,该格式说明符适用于 signed int。请改用 %f%g%e

另外,为什么不这样做呢:

    destination->x = a->x + b->x;

这样对眼睛来说更容易。 (虽然不是问题)。

You are printing float with format specifier %d which is intended for signed int. Use %f or %g or %e instead .

Also, why don't you do:

    destination->x = a->x + b->x;

Its much easier on the eyes. (although not a problem).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文