alsa_test.h头文件包含以下代码:
struct alsa_device;
typedef struct alsa_device alsa_device;
alsa_device *create_alsa_array(size_t n);
alsa_test.c 文件包括:
struct alsa_device
{
int card;
int dev;
char info[80];
};
typedef struct alsa_device alsa_device;
alsa_device *create_alsa_array(size_t n) {
alsa_device *new = malloc(n * sizeof *new);
return new;
};
我的 main.c 文件具有以下内容:
#include "alsa_test.h"
int main (int argc, char *argv[]) {
alsa_device *devices;
create_alsa_array(devices,50);
devices[0]->card=1;
}
我收到以下错误
error: invalid use of undefined type 'struct alsa_device'
有什么想法吗?
最佳答案
您的问题出在 main()
中
devices[0]->card=1;
此时devices
是一个指向没有定义的结构体的指针。
如果您想保持结构未定义,请在 alsa_test.c 中定义一个函数,该函数接受指向它的指针和一个整数(请记住在头文件中添加原型(prototype))
void setcard(struct alsa_device *dst, size_t index, int card) {
dst[index].card = card;
}
并从main()
调用它
setcard(devices, 0, 1); // set card of device 0 to 1
setcard(devices, 1, 42); // set card of device 1 to 42
关于c - 使用 typedef 结构时无效使用未定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757360/