2012-05-27 36 views
1


老實說,我不知道如何發佈此問題...
我有一個全球性的動態列表是通過程序的所有功能可見,
今天,因爲這入榜從文件中填充讀取數據。
但現在我想有一個內部的「列表」,如果沒有指定文件,則加載它。
列表元素是這樣的:
動態列表

// odb tuple 
typedef struct _odb_t 
{ 
const char *name,*value; 
struct _odb_t *next; 
} odb_t; 

enum _method {GET,POST}; 

// odb type binding 
typedef struct _odb_type 
{ 
    enum _type type; 
    const char *value; 
    struct _odb_type *next; 
} odb_type; 

// defining online_db struct 
typedef struct _odb 
{ 
    const char *host,*file,*patrn; 
    enum _method method; 
    odb_type *types; 
    odb_t *tuples; 
    pthread_t thread; // the thread that is using this host 
    struct _odb *next; 
} odb; 

我怎麼能有一個內部列表到.text段?
在此先感謝。

回答

2

在C99中,您可以將所謂的複合文字作爲某種未命名的變量和指定的初始化工具,以簡化初始化工具的編寫。你的結構有點複雜,我可以在第一眼看到它們的完整含義,但類似於這裏的東西應該可以工作。

odb const*const head = 
&(odb const){ 
    .method = something, 
    .next = &(odb const){ 
    .method = another, 
    .next = 0, 
    }, 
}; 

當然,你將不得不用正確的數據初始化類似其他指針領域,但我希望你的想法。

在文件範圍內使用時,靜態分配形式爲(typename){ initiliazers }的複合文字。

+0

嗨,感謝您的回覆。 2件事: 1)'const * const'是錯誤的權利?! 2)爲什麼'const'在'odb'之後? 再次感謝! –

+0

這是一個指向不可變對象的不可變指針。如果你需要'const'或不是由你決定。你的問題不夠精確,不知道你是否希望這個列表是可以修改的。 –

+0

啊,在類型之後的'const'是一種可能性。我總是發現它更容易閱讀,因爲如果你這樣寫,'const'總是適用於左邊寫的東西。 –