我有一個程序,它需要爲字一些文本字加載到一個數組,所以我有用於傳遞指向struct功能之間陣列
main.h定義的每個文本中的結構
typedef struct wordTag{
char name[MAX_WORD];
char string[1000][MAX_WORD];
int words;
}text;
的main.c
void main(){
int fileCount = 0;
text *checkTexts;
fileCount = getCheckFiles(checkTexts);
for(i = 0; i < fileCount; i++){
printf("Tekst navn: %s\n", checkTexts[i].name);
}
}
file.c
int getCheckFiles(text *checkTexts){
int fileCount, i;
FILE *file;
createFileList(&file);
fileCount = countFiles(file);
createArray(checkTexts, fileCount, file);
return fileCount;
}
void createArray(text *checkTexts, int fileCount, FILE *file){
int i, wordCount;
FILE *textFile;
text localText;
char fileName[MAX_WORD + 30];
checkTexts= (text *)malloc(fileCount * sizeof(text));
readFileNames(file, checkTexts);
for(i = 0; i < fileCount; i++){
localText = checkTexts[i];
strcpy(fileName, "./testFolder/");
strcat(fileName, localText.name);
openFile(&textFile, fileName);
localText.words = countWords(textFile);
readFileContent(textFile, localText);
checkTexts[i] = localText;
}
for(i = 0; i < fileCount; i++){
printf("Tekst navn: %s\n", checkTexts[i].name);
}
}
現在,如果我在createArray功能打印名稱每一件事工作正常,但如果我嘗試和打印在我的主要功能我得到一個分段錯誤(核心轉儲)。
只是一個提示:瞭解'gdb',你永遠不會有詢問有關分段錯誤的錯誤的人......你可以自己跟蹤一下,看看究竟在何處的問題是... – mtahmed