2015-07-10 52 views
0

我想从旧库的代码中删除以下警告我的工作:GCC:“警告:分配从兼容的指针类型”

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

我shortend在下面的文本代码使它更易于阅读!

现在,我认为get_pixel1是一个函数指针,这个函数:

#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; 
} 

虽然f.get_pixel在这里被定义:

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; 

我的问题是什么我要在这里投删除问题标题中的警告:

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

或者除了ca外还有其他的事情要做吗? ST?

+0

可能是您的函数指针需要signed int,而您的函数使用unsigned int。 –

回答

3

在结构中您有:

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

为你申报你的函数:

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

的不匹配是在第二个和第三个参数的unsigned,无论是在结构体成员加入他们或从函数定义中删除它们。

另外一般情况下,您不应该将函数指针强制转换为另一种类型的函数指针,因为它会导致未定义的行为。所以如果你发现自己在做这样的事情:

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

可能有更好的解决方案。有关更多详细信息,请参阅此SO question

+0

我添加了无符号,并没有更多的警告。无符号是正确的,因为屏幕上没有负像素位置。我不会在这里看到这个缺陷,所以非常感谢你的帮助! – Georg

相关问题