我从mysql数据库获取值,我想用返回的每一行来组织它。这里是我的结构(例如只):C动态数组中的分段错误
typedef struct
{
char* name;
char* etc;
int state;
} person;
和MySQL:
MYSQL * con;
mysql_connect(&con); //connect to mysql database and set handle to con variable.
MYSQL_ROW row;
MYSQL_RES * result;
int num_fields, i;
mysql_query(con, "select name,etc,state from db.tbl");
result = mysql_store_result (con);
num_fields = mysql_num_fields (result);
person tempdata;
person personlist[num_fields * sizeof(person)]; //the size if the problem, I believe...
while((row = mysql_fetch_row (result))) {
tempdata.name = row[0];
tempdata.etc = row[1];
tenpdata.state = atoi(row[2]);
personlist[i++] = tempdata; // the error line
}
mysql_free_result (result);
mysql_close (con);
但它返回Segmentation fault
如何解决这一问题?提前致谢。
使用调试器(例如gdb)并尝试找到发生分段错误的行。 – Zeta 2012-02-27 14:42:07
分段故障发生在哪里?我们无法复制您的整个测试设置,因此您提供的任何和所有信息都只能帮助您。 – tbert 2012-02-27 14:43:11