2014-03-30 59 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 }; 

但不管是令人滿意取決於你試圖實現。