Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

c - "Assignment from incompatible pointer type" warning

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
   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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...