我從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