2015-07-10 85 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