2012-01-06 95 views
1

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

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

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]); 

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

+0

'(*阿尼姆)[I] .next'是一个指向一个'结构动画'; '&((* anim)[* tmp])'是一个'animation'的地址,一个没有标签的'struct'。 – pmg 2012-01-06 10:04:23

回答

9
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;)任何提到“结构动画”结构定义内部是一个参考,到目前为止,还没有定义的结构。由于您只使用指向该未定义结构的指针,因此编译器不介意。

所以,添加标记---甚至可能摆脱的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; 
+0

多么完美的解释!这立即解决了问题!谢谢! – eyebrowsoffire 2012-01-06 09:46:19

+0

我不会说typedef增加了混乱,因为它的目的是在代码的其余部分去除混乱,但我认为它们通常被过度使用。当使用struct关键字时,我发现代码更容易阅读,尤其是在编辑器中没有针对自定义类型的智能语法高亮显示。 – yak 2014-09-29 02:46:51

相关问题