出於某種原因,當我嘗試運行此測試代碼時,出現分段錯誤。該程序應該從文件中讀取字符串並將其放入數組中。我是C新手,嘗試使用調試器,但遇到問題。將字符串從文件讀入數組
任何輸入將不勝感激。
void fillArray(char *array[], int * count, FILE * fpin){
char buf[40];
char *p;
count = 0;
while(fgets(buf, 40, fpin) != NULL){
if((p= strchr(buf, '\n')) != NULL)
*p = '\0'; //step on the '\n'
array[(*count)++] = malloc(strlen(buf)+1);
assert(array[*count]);
strcpy(array[*count], buf);
(*count)++;
}
}
它做什麼,而不是你認爲它應該做什麼? [除了'count = 0'的明顯錯字應該是'* count = 0',那就是?] – 2013-03-14 20:53:54
另外,還有一個語義錯誤:你正在增加'* count'兩次。 – 2013-03-14 20:54:25
我已經修復了這個錯字,貼錯了,對不起。無論如何,現在的代碼在試圖在測試文件中運行時會出現分段錯誤錯誤。 – silentman45 2013-03-14 20:57:21