これまでの質問のスピンとして、私は3次元配列にメモリを割り当てるといういくつかの問題にぶつかってきました。3次元のchar配列にメモリを割り当てると、セグメンテーションフォルトが発生する
私はテキストを使って作業する必要があるプロジェクトに取り組んでいます。これを行うには、テキストをより小さなセクションに分割し、単語のテキストワードを処理する必要があります。これらの小さなテキストを保存するには、セクション内の単語のリストを含むセクションのリストである3D配列があります。
しかし、malloc()
を使用して個々の単語のメモリを割り当てようとすると、セグメント化エラーが発生します。
localText->list[i][n] = malloc(100 * sizeof(char));
ここにコード全体があります。
typedef struct {
char name[100];
char ***list;
}text;
int main(){
int i = 0, n, z,wordCount, sections;
FILE *file;
text *localText;
openFile(&file, "test.txt");
wordCount = countWords(file);
sections = (wordCount/50) + 1;
localText = malloc(sizeof(text));
localText->list = malloc(sections * sizeof(char **));
for(i = 0; i < sections; i++)
localText->list[i] = malloc(50 * sizeof(char *));
for(n = 0; n < 50; n++)
localText->list[i][n] = malloc(100 * sizeof(char));
readFileContent(file, localText->list, 50);
freeText(localText);
return 1;
}
W/Oは、唯一の文は、ループ本体に属しブレース。括弧を外してはいけません! :-) – ckruse