“来自不兼容指针类型的分配”警告

发布于 2024-12-25 08:59:48 字数 742 浏览 0 评论 0原文

我正在编写一个函数,它解析带有纹理和动画数据的文件并将其加载到我声明的一些全局结构中。我在特定行上收到编译器警告“来自不兼容指针类型的赋值”。代码很多,所以我只在这里发布重要的部分。

首先,我的动画例程有一个结构数据类型,如下所示:

    typedef struct {
        unsigned int frames;
        GLuint *tex;
        float *time;
        struct animation *next;
    } animation;

如您所见,结构中的最后一个变量是指向另一个动画的指针,默认为动画完成时的位置。

以下是加载函数的声明:

    void LoadTexturePalette(GLuint **texture, animation **anim, const char *filename)

该函数将信息加载到动画数组中,因此是双指针。

在加载每个动画的最后,将从文件中提取一个整数,该整数指示“下一个”指针将指向哪个动画(在加载的动画中)。

    fread(tmp, 1, 4, file);
    (*anim)[i].next = &((*anim)[*tmp]);

在最后一行,我收到编译器警告。我还没有使用该变量,所以我不知道警告是否是一个问题,但我感觉我的语法或方法在设置该变量时可能不正确。

I'm writing a function that parses a file with texture and animation data and loads it into some global structs I have declared. I get the compiler warning "Assignment from incompatible pointer type" on a particular line. It's a lot of code, so I'm just going to post the important parts here.

First, I have a struct datatype for my animation routines, as follows:

    typedef struct {
        unsigned int frames;
        GLuint *tex;
        float *time;
        struct animation *next;
    } animation;

As you can see, the last variable in the struct is a pointer to another animation to default to when the animation is completed.

Here is the declaration of the loading function:

    void LoadTexturePalette(GLuint **texture, animation **anim, const char *filename)

The function loads information into an array of animations, hence the double pointer.

At the very end of loading each animation, an integer is pulled from the file that indicates which animation (out of the ones that are loaded) to which the "next" pointer will point.

    fread(tmp, 1, 4, file);
    (*anim)[i].next = &((*anim)[*tmp]);

On the last line, I get the compiler warning. I'm not yet using that variable, so I don't know if the warning is an issue, but I get the feeling that my syntax or my approach may be incorrect in setting that variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

能否归途做我良人 2025-01-01 08:59:48
 typedef struct { /* 定义中没有标记 */
       无符号整型帧;
       GLuint *tex;
       浮动*时间;
       结构动画*下一个; /* 指向未定义结构的指针 */
   } 动画片;

如果没有标签 (typedef structanimation { /* ... */ }animation;),结构体定义中对“structanimation”的任何引用都是对尚未定义的结构体的引用。由于您只使用指向未定义结构的指针,因此编译器不介意。

因此,添加标签 --- 甚至可能摆脱 typedef:它只会增加混乱:)

    typedef struct animation { /* tag used in definition */
        unsigned int frames;
        GLuint *tex;
        float *time;
        struct animation *next; /* pointer to another of this structure */
    } animation;
   typedef struct { /* no tag in definition */
       unsigned int frames;
       GLuint *tex;
       float *time;
       struct animation *next; /* pointer to an undefined structure */
   } animation;

Without the tag (typedef struct animation { /* ... */ } animation;) any reference to "struct animation" inside the struct definition is a reference to an, as yet, undefined structure. As you only use a pointer to that undefined structure, the compiler doesn't mind.

So, add the tag --- and maybe even get rid of the typedef: it only adds clutter :)

    typedef struct animation { /* tag used in definition */
        unsigned int frames;
        GLuint *tex;
        float *time;
        struct animation *next; /* pointer to another of this structure */
    } animation;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文