C 文本部分中的动态列表
C 文本部分中的动态列表,c,C,老实说,我不知道如何发布这个问题… 我有一个全局动态列表,可以被程序的所有功能看到, 从今天起,此列表是从文件读取数据填写的。 但是现在我想有一个内部“列表”,如果没有指定文件,它将被加载。 列表元素如下所示: // odb tuple typedef struct _odb_t { const char *name,*value; struct _odb_t *next; } odb_t; enum _method {GET,POST}; // odb type binding typed
老实说,我不知道如何发布这个问题…
我有一个全局动态列表,可以被程序的所有功能看到,
从今天起,此列表是从文件读取数据填写的。
但是现在我想有一个内部“列表”,如果没有指定文件,它将被加载。
列表元素如下所示:
// 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部分?提前感谢。使用C99,您可以将所谓的复合文本作为某种未命名变量和指定的初始值设定项,以简化初始值设定项的编写。你们的结构有点复杂,我一眼就能看出它们的全部含义,但类似于这里的东西应该会起作用
odb const*const head =
&(odb const){
.method = something,
.next = &(odb const){
.method = another,
.next = 0,
},
};
当然,您必须用正确的数据类似地初始化其他指针字段,但我希望您能理解
在文件作用域中使用时,形式为
(typename){initilizers}
的复合文本是静态分配的。hi,感谢您的回复。2件事:1)常量*常量是一个错误,对吗?!2) 为什么const
在odb
之后?再次感谢!这是指向不可变对象的不可变指针。是否同时需要const
取决于您。您的问题不够精确,无法知道您是否希望该列表可修改。啊,类型后的const
是一种可能性。我总是发现它更容易阅读,因为如果你这样写的话,const
总是适用于它左边写的东西;再次感谢:)