我想在C中創建一個固定長度的「字符串」數組,但一直有一點麻煩。我遇到的問題是我得到了分段錯誤。如何在C中創建固定長度的「字符串」數組?
這裏是我的程序的目標:我想通過使用從文本文件讀取的數據索引設置數組的字符串。這裏是我當前的代碼要旨(我很抱歉,我不能加入我的整個代碼,但它是相當漫長的,而且很可能只是造成混淆):
//"n" is set at run time, and 256 is the length I would like the individual strings to be
char (*stringArray[n])[256];
char currentString[256];
//"inputFile" is a pointer to a FILE object (a .txt file)
fread(¤tString, 256, 1, inputFile);
//I would like to set the string at index 0 to the data that was just read in from the inputFile
strcpy(stringArray[i], ¤tString);
您將'stringArray'聲明爲由n個指向256個字符數組的指針組成的數組。這聽起來不正常......「正常」。 –
也許它已經在這裏回答了? [C程序:使用字符串數組的新手混淆](http://stackoverflow.com/questions/10055946/c-program-newbie-confusion-on-working-with-character-string-arrays) – jayant
@JoachimPileborg是,我發現有點奇怪,以及..這是基於第二個答案在這裏:http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in -c – user3495690