我有这个功能,在那里我获取数据库的数据并制作一个链表。列表的每个节点都是一行。
这是功能
Row * fetch(Query* query)
{
if (PQresultStatus(query->resultset) != PGRES_TUPLES_OK)
return NULL;
Row * row;
row = (Row *) malloc(sizeof (Row));
row->total_cols = PQnfields(query->resultset);
int rows = PQntuples(query->resultset);
int row_atual, col_atual;
Row * aux;
aux = row;
for (row_atual = 0; row_atual < rows; row_atual++) {
for (col_atual = 0; col_atual < aux->total_cols; col_atual++) {
aux->cell[col_atual] = PQgetvalue(query->resultset, row_atual, col_atual);
}
aux->next_line = (Row *) malloc(sizeof(Row));
aux = aux->next_line;
}
return row;
}
这是结构行:
typedef struct row {
int total_cols;
char ** cell;
struct row * next_line;
} Row;
问题是当它到达这一点时:
aux->next_line = (Row *) malloc(sizeof(Row));
我收到了分段错误,我不知道为什么!我看不出有什么问题。有人知道吗?
谢谢!