2

在头文件中:

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);
4

3 回答 3

2

void * kmalloc (size_t size, int flags);

kmallocvoid *您尝试分配的返回类型contain不正确。

改成,

typedef struct {
   contain *info; // info is a pointer to struct contain
} query_arg_t;

query_arg_t q;
q.info = kmalloc(sizeof(_vector), GFP_KERNEL);

回答后续问题:

q.info是指向结构的指针contain。您需要数组运算符->通过指针访问结构成员。因此,请尝试以下选项访问info

q.info->stat.t.option

或者

(*q.info).stat.t.option
于 2014-02-07T09:38:03.477 回答
1

你的结构是这样的

typedef struct {
       contain *info;
} query_arg_t;

您正在尝试将指针 ( kmallocreturns void *) 分配给struct变量 ( contain)。

于 2014-02-07T09:58:59.627 回答
0

C 语言规范禁止在其他结构中声明结构类型而不声明该类型的成员。

于 2014-02-07T09:37:08.310 回答