如何设置结构数组的 "end"
How to set the "end" of an array of structs
我想将 [=12=]
的等价物设置到结构数组的末尾,以便我知道它何时结束。
到目前为止,我正在尝试做这样的事情:
typedef struct {
char * id;
char * date;
} ROW;
int main(int argc, char *argv[]) {
FILE * fp = fopen("test100k.csv", "r");
ROW * rows = malloc(sizeof(row) * 10000);
int row_num = 0;
ROW row;
char buffer[255];
while(fgets(buffer, sizeof(buffer), fp) != NULL) {
// some logic to set the object
rows[row_num++] = row;
}
rows[row_num] = NULL; // how to do this?
}
"end" 行数组的正确方法是什么?
你可以尝试使用memset:
memset(&rows[row_num], NULL, sizeof(ROW));
一种可能性是定义一个常量,该常量通过其成员的唯一值充当哨兵:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char * id;
char * date;
} ROW;
/* Initialising the ROW's members below to a literal could lead to
ambiguities, as the compiler is free to merge the same literal
appearing more then once into only one instance in memory.
Whereas initialising the members to the struct's address itself
guarantees the uniqueness of the address stored.
(Just do not ever dereference it, at least not without casting it
back to the type ROW.) */
const ROW STOPPER_ROW = {(char*)&STOPPER_ROW, (char*)&STOPPER_ROW};
int main(int argc, char *argv[]) {
...
ROW * rows = malloc(10000 * sizeof *rows);
size_t row_num = 0;
...
while(NULL != fgets(buffer, sizeof(buffer), fp) &&
10000 > row_num) {
// some logic to set the object
rows[row_num++] = row;
}
rows[row_num] = STOPPER_ROW;
row_num = 0;
/* Comparing only one member below in fact would do. */
while (rows[row_num].id != STOPPER_ROW.id &&
rows[row_num].date != STOPPER_ROW.date)
{
/* Use rows[row_num] here */
++row_num;
}
}
这些是标准方法:
- 添加一个指示最后一个有效索引的计数器变量。
- 按引用而不是按值存储行。即,为每一行分配一个 space 并将指向它的指针存储在数组中,而不是存储行本身。如果指针为空,则该行无效。
- 如果某行存在已知的无效值,则将所有数组单元初始化为该值。具有无效值的单元格将指示有效项目结束。
我想将 [=12=]
的等价物设置到结构数组的末尾,以便我知道它何时结束。
到目前为止,我正在尝试做这样的事情:
typedef struct {
char * id;
char * date;
} ROW;
int main(int argc, char *argv[]) {
FILE * fp = fopen("test100k.csv", "r");
ROW * rows = malloc(sizeof(row) * 10000);
int row_num = 0;
ROW row;
char buffer[255];
while(fgets(buffer, sizeof(buffer), fp) != NULL) {
// some logic to set the object
rows[row_num++] = row;
}
rows[row_num] = NULL; // how to do this?
}
"end" 行数组的正确方法是什么?
你可以尝试使用memset:
memset(&rows[row_num], NULL, sizeof(ROW));
一种可能性是定义一个常量,该常量通过其成员的唯一值充当哨兵:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char * id;
char * date;
} ROW;
/* Initialising the ROW's members below to a literal could lead to
ambiguities, as the compiler is free to merge the same literal
appearing more then once into only one instance in memory.
Whereas initialising the members to the struct's address itself
guarantees the uniqueness of the address stored.
(Just do not ever dereference it, at least not without casting it
back to the type ROW.) */
const ROW STOPPER_ROW = {(char*)&STOPPER_ROW, (char*)&STOPPER_ROW};
int main(int argc, char *argv[]) {
...
ROW * rows = malloc(10000 * sizeof *rows);
size_t row_num = 0;
...
while(NULL != fgets(buffer, sizeof(buffer), fp) &&
10000 > row_num) {
// some logic to set the object
rows[row_num++] = row;
}
rows[row_num] = STOPPER_ROW;
row_num = 0;
/* Comparing only one member below in fact would do. */
while (rows[row_num].id != STOPPER_ROW.id &&
rows[row_num].date != STOPPER_ROW.date)
{
/* Use rows[row_num] here */
++row_num;
}
}
这些是标准方法:
- 添加一个指示最后一个有效索引的计数器变量。
- 按引用而不是按值存储行。即,为每一行分配一个 space 并将指向它的指针存储在数组中,而不是存储行本身。如果指针为空,则该行无效。
- 如果某行存在已知的无效值,则将所有数组单元初始化为该值。具有无效值的单元格将指示有效项目结束。