我完全卡在了这一点上。我在 3 个文件中有以下代码:
文件 混音器_oss.c
#include "mixer.h"
static char *devices[] = SOUND_DEVICE_NAMES;
static char **oss_get_device(void)
{
int i, o, devs, res;
char **result;
if ((ioctl(fd, SOUND_MIXER_READ_RECMASK, &devs)) == -1) {
return NULL;
} else {
result = malloc(sizeof(char*)*SOUND_MIXER_NRDEVICES);
o = 0;
for (i=0; i < SOUND_MIXER_NRDEVICES; i++) {
res = (devs >> i)%2;
if (res) {
result[o] = malloc(strlen(devices[i])+1);
sprintf(result[o], "%s", devices[i]);
o++;
}
result[o] = NULL;
}
}
return result;
}
struct mixer oss_mixer = {
.get_device = oss_get_device,
};
文件 混频器.h
#ifdef __cplusplus
extern "C" {
#endif
struct mixer
{
char (* get_device) (void);
};
#pragma weak oss_mixer
extern struct mixer oss_mixer;
#ifdef __cplusplus
};
#endif
文件 混频器.c
#include "mixer.h"
static char null_get_device(void)
{
}
static struct mixer *mixers[] = {
&oss_mixer,
&null_mixer
};
现在,当我编译代码时,这就是我得到的
mixer-oss.c: warning: initialization from incompatible pointer type [enabled by default]
.get_device = oss_get_device,
^
warning: (near initialization for ‘oss_mixer.get_device’) [enabled by default]
你能帮帮我吗?
提前致谢。
最佳答案
char (* get_device) (void);
声明一个指向不带参数并返回 char
的函数的指针。
static char **oss_get_device(void)
是一个不带参数并返回指向 char
指针的函数。
你的函数指针应该这样声明:
char ** (*get_device)(void);
现在它与oss_get_device
兼容。
关于c - 从不兼容的指针类型(指向函数的指针)初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047653/