[英]gcc: “warning: assignment from incompatible pointer type”
我想从我正在使用的旧库的代码中删除以下警告:
Image.c:171:22: warning: assignment from incompatible pointer type [enabled by default]
image->f.get_pixel = get_pixel1;
我缩短了以下文本中的代码,以使其更易于阅读!
现在,我认为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;
还是除了演员外还有其他事情要做?
在结构中,您具有:
unsigned long (*get_pixel) (struct _XImage *, int, int);
您将函数声明为:
static unsigned long
get_pixel1(XImage *image, unsigned int x, unsigned int y)
不匹配是第二个和第三个参数中的unsigned
,可以将它们添加到struct成员中,或者将它们从函数定义中删除。
同样,通常也不应将函数指针转换为其他类型的函数指针,因为这会导致未定义的行为。 因此,如果您发现自己正在执行以下操作:
image->f.get_pixel = (?????)get_pixel1;
可能有更好的解决方案。 有关更多详细信息,请参见此SO问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.