2014-03-30 60 views
1

我试图在linux内核中学习更多关于kobject的知识,并且在尝试编写使用这种设施的模块时,我收到错误和警告消息,所以我已经在这里放下了相关数据结构的修剪版本以及相应的gcc的错误和警告信息。指定的初始化程序:GCC警告消息:临近初始化和其他警告消息

$ gcc issue.c 
issue.c:30:1: error: initializer element is not constant 
} ; 
^ 
issue.c:30:1: error: (near initialization for ‘first.attr’) 
issue.c:34:1: error: initializer element is not constant 
}; 
^ 
issue.c:34:1: error: (near initialization for ‘second.attr’) 
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default] 
struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 
     ^
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[0]’) [enabled by default] 
issue.c:39:8: warning: initialization from incompatible pointer type [enabled by default] 
issue.c:39:8: warning: (near initialization for ‘my_bin_attrs[1]’) [enabled by default] 

和样例代码:

#include <stdio.h> 

struct attribute { 
    const char  *name; 
    unsigned short   mode; 
}; 

struct bin_attribute { 
    struct attribute attr; 
    unsigned int  size; 
    void   *private; 
}; 

struct attribute_group { 
    const char  *name; 
    struct attribute **attrs; 
    struct bin_attribute **bin_attrs; 
}; 

struct attribute first_attr = { 
    .name = "FIRST" 
}; 

struct attribute second_attr = { 
    .name = "SECOND" 
}; 

struct bin_attribute first = { 
    .attr = first_attr 
} ; 

struct bin_attribute second = { 
    .attr = second_attr 
}; 

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 

int main(void) 
{ 
    struct attribute_group my_group = { 
     .name = "xyz", 
     .bin_attrs = my_bin_attrs, 
    }; 

    return 0; 
} 
+0

[尝试使用const初始化变量时,错误“initializer元素不是常量”的可能重复(http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-试图初始化变量-W) – Dukeling

+0

当你收到一条错误信息时,你应该** !!总是!! **谷歌错误消息试图找出它,然后再问一个关于它的问题。 – Dukeling

+0

“和其他警告信息”当然不是理想的问题。一个问题应该是关于一个错误(/问题)(并且它们是错误的,而不是警告 - 这是有区别的)。 [so]上的帖子旨在为许多用户提供长期价值。如果你通过询问许多事情来污染帖子,任何人在寻找特定帖子时遇到这个帖子的机会都很渺茫,如果他们这样做了,他们将不得不筛选所有其他不适用的信息到其他错误。 – Dukeling

回答

0

不能使用一个变量的值作为一个文件范围初始化的初始化。

您有:

struct attribute first_attr = { 
    .name = "FIRST" 
}; 

… 

struct bin_attribute first = { 
    .attr = first_attr 
}; 

,因为它试图使用表达式,first_attr二是产生了“非常量初始化”的错误,这不是一个编译时间常数。你可能会在C++中脱离它;你不能在C.

你可以使用一个变量的地址;它被视为编译时常量(在链接时解析),而不是变量的值。在函数内部,你也可以。

重新设计struct bin_attribute以便attr成员是一个指针,或者放弃您显示的初始化。

第二组误差是由于:

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first_bin_attrs, &second_bin_attrs }; 

类型的&first_bin_attrsstruct bin_attribute **,所以有一个在初始化值指针的级别太多。如果你删除了&,类型是正确的,但你又回到了“非常量初始化器”的问题。如果将*添加到my_bin_attrs的类型中,则有可能变为Three Star Programmer,您将不得不修改使用my_bin_attrs的代码。

简单的解决方法是:

struct bin_attribute *first_bin_attrs = &first; 
struct bin_attribute *second_bin_attrs = &second; 

struct bin_attribute *my_bin_attrs[] = { &first, &second }; 

但不管是令人满意取决于你试图实现。