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'
任何想法为什么?
答案 0 :(得分:2)
您的问题出在main()
,此处
devices[0]->card=1;
此时devices
是指向没有定义的结构的指针。
如果你想保持结构未定义,在alsa_test.c中定义一个函数,它接受一个指针和一个整数(记得在你的头文件中添加一个原型)
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
答案 1 :(得分:1)
您有两个选择:
添加alsa_test.h
void set_alsa_card(struct alsa_device * alsa_dev_list, int dev_id, int card);
和alsa_test.c
void set_alsa_card(struct alsa_device * alsa_dev, int dev_id, int card)
{
(alsa_dev + dev_id)->card = card;
}
main.c:
#include "alsa_test.h"
int main (int argc, char *argv[]) {
alsa_device *devices;
devices = create_alsa_array(50);
set_alsa_card(devices, 0, 1);
}