Georg
Georg

Reputation: 147

gcc: "warning: assignment from incompatible pointer type"

I would like to remove the following warning from the code of an old library I am working on:

Image.c:171:22: warning: assignment from incompatible pointer type [enabled by default]
   image->f.get_pixel = get_pixel1;

I shortend the code in the following text to make it easier to read!

Now, I think get_pixel1 is a function pointer to this function:

#define READ_BIT(image, x, y)   \
    (image->data[(y * image->bytes_per_line) + (x >> 3) ] & (1 << (x & 7)))

static unsigned long
get_pixel1(XImage *image, unsigned int x, unsigned int y)
{
    return READ_BIT(image, x, y) != 0;
}

While f.get_pixel is defined in here:

typedef struct _XImage {
    int width, height;      /* size of image */
    /* snip */
    struct funcs {      /* image manipulation routines */
    struct _XImage *(*create_image)( /*snip*/ );
    /* snip */
    unsigned long (*get_pixel)  (struct _XImage *, int, int);
    /* snip */
    } f;
} XImage;

My question is what do I have to cast here to remove the warning in the question's title:

    image->f.get_pixel = (?????)get_pixel1;

Or is there something additional to do except the cast?

Upvotes: 0

Views: 916

Answers (1)

missimer
missimer

Reputation: 4079

In the struct you have:

unsigned long (*get_pixel)  (struct _XImage *, int, int);

You declared your function as:

static unsigned long
get_pixel1(XImage *image, unsigned int x, unsigned int y)

The mismatch is the unsigned in the second and third parameters, either add them in the struct member or remove them from the function definition.

Also in general you should not cast a function pointer to a function pointer of another type as it results in undefined behavior. So if you ever find yourself doing something like this:

image->f.get_pixel = (?????)get_pixel1;

There is probably a better solution. See this SO question for more detail.

Upvotes: 3

Related Questions