在头文件中:
typedef struct {
char* a;
int allowed;
struct suit {
struct t {
char* option;
int count;
} t;
struct inner {
char* option;
int count;
} inner;
} suit;
} contain;
typedef struct {
contain info;
} query_arg_t;
在内核模块中,
// 初始化
static const contain _vector = {
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF (*)",
.count = 7
}
}
};
但是,当我们尝试:
query_arg_t q;
q.info = kmalloc(sizeof(_vector), GFP_KERNEL);
我们会得到这个错误:error: incompatible types when assignment to type 'contain' from type 'void *'</p>
上述错误由@SunEric 和@Sakthi Kumar 解决。
q.info = kmalloc(sizeof(_vector), GFP_KERNEL);
memcpy(&(q.info), &(_vector), sizeof(_vector));
现在看来还可以。它会构建,但是当它运行到该部分时,它会声明内核堆栈已损坏。尝试执行后:
printf("option: %s \n", q.info->suit.t.option);
printf("option: %s \n", q.info->suit.t.option);
[更新:已解决]
@Sakthi Kumar 通过以下方式解决了它:
//removing kmalloc
// commenting out: q.info = &_vector;
memcpy(&(q.info), &(_vector), sizeof(_vector));
printf("option: %s \n", q.info.suit.t.option);
printf("option: %s \n", q.info.suit.inner.option);