c - 从不兼容的指针类型(指向函数的指针)初始化

标签 c arrays pointers

我完全卡在了这一点上。我在 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/

相关文章:

c - 跟踪递归函数

shell : Execvp prints output and then segmentation fault occurs when executing a background process 的 C 程序

c# - 来自 ReadOnlyCollection<T> 的对象 []

c - 释放已用数据的内存会导致段错误

C - fgets 在初始化指针时不等待输入

c - Valgrind:大小 1 的无效读取

PHP:收集传递给函数的所有变量作为数组?

php - 如何从数据库的平面数组中创建嵌套注释数组

c++ - C 和 C++ : Array element access pointer vs int

c - scanf 获取之前执行的 getchar() 的输入