注意:我已经重新编写了原始问题以使其更加清晰。
我有一个名为
VcStatus readVcard( FILE *const vcf, Vcard **const cardp )
vcf 是我将读取的打开文件,而 cardp 是指向卡片数组开头的指针。一个文件将包含多张卡片。
readVCard 每次读取文件一行,并调用函数 parseVcProp 来识别该行中的关键字,并将它们分配到结构中的适当位置。
这里是结构
typedef struct { // property (=contentline)
VcPname name; // property name
// storage for 0-2 parameters (NULL if not present)
char *partype; // TYPE=string
char *parval; // VALUE=string
char *value; // property value string
void *hook; // reserved for pointer to parsed data structure
} VcProp;
typedef struct { // single card
int nprops; // no. of properties
VcProp prop[]; // array of properties
} Vcard;
typedef struct { // vCard file
int ncards; // no. of cards in file
Vcard **cardp; // pointer to array of card pointers
} VcFile;
所以一个文件包含多个卡片,一个卡片包含多个属性,等等。
问题是,一张卡片可以有任意数量的属性。在你读完之前不知道有多少。
这是我不明白的。
我必须如何分配内存才能正确使用 parseVcProp?
每次我调用 parseVcProp 时,我显然希望它将数据存储在一个新的结构中,那么我如何事先分配这个内存呢?我只是 malloc(sizeof(VcProp)*1) 吗?