我必须使用define创建结构数组。请建议我怎么做。
file1.c have data
{
"string1",
13
}
file2.c have data
{
"string2",
14
}
需要将此数据传递给一个公共定义,以便它可以填充如下数据:
struct test data[]=
{
{"string1", 13},
{"string2", 14}
}
让我在这里详细说明:
file1.c
ADD_DATA_IN_STRUCT(....) ---> pass data {"string1", 13}
file2.c
ADD_DATA_IN_STRUCT(....) ---> pass data {"string1", 14}
main.h
#define ADD_DATA_IN_STRUCT(....) ---> should expand to
struct test data[]=
{
{"string1", 13},
{"string2", 14}
}
答案 0 :(得分:1)
希望它有所帮助,我遗漏了细节。
// 1. define the test struct
define struct test {
char *string;
int number;
};
// 2. define the data array
struct test data[2];
// 3. read file, assign value to corresponding fields in data array
// you may want to dynamically allocated memory for `string` using `malloc`
// or if the string has a maximum size, define it using `char string[MAX_SIZE]` instead