“从不兼容的指针类型赋值"警告 [英] "Assignment from incompatible pointer type" warning

查看:32
本文介绍了“从不兼容的指针类型赋值"警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,该函数解析包含纹理和动画数据的文件,并将其加载到我声明的一些全局结构中.我在特定行上收到编译器警告从不兼容的指针类型赋值".代码很多,所以我只把重要的部分贴在这里.

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.

在加载每个动画的最后,从文件中提取一个整数,指示next"指针将指向哪个动画(在已加载的动画中).

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.

推荐答案

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

如果没有标签 (typedef struct animation {/* ... */} animation;),在结构定义中对struct 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.

所以,添加标签 --- 甚至可以去掉 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;

这篇关于“从不兼容的指针类型赋值"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆