如何在C中为char **动态分配内存

我将如何在此函数中动态分配内存到char **列表?

基本上这个程序的想法是我必须从文件中的单词列表中读取。 我不能假设最大字符串或最大字符串长度。

我必须用C字符串做其他的东西,但那些东西我应该没问题。

谢谢!

void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile = fopen(argv[1], "r"); if(!myFile) { printf("No such file or directory\n"); exit(EXIT_FAILURE); } while((c = fgetc(myFile)) !=EOF) { numberOfChars++; if(c == '\n') { if(maxNumberOfChars < numberOfChars) maxNumberOfChars += numberOfChars + 1; numberOfLines++; } } list = malloc(sizeof(char*)*numberOfLines); for(i = 0; i  0) { list[wordLine][counter] = '\0'; wordLine++; counter = 0; } else if(c != '\n') { list[wordLine][counter] = c; counter++; } } } 

这样做:

 char** list; list = malloc(sizeof(char*)*number_of_row); for(i=0;i 

此外,如果您动态分配内存。 你要完成它的工作:

 for(i=0;i 

编辑

在你修改过的问题中:

  int wordLine = 0, counter = 0, i; 

wordLinecounter0

在此代码之前:

 list = malloc(sizeof(char*)*wordLine+1); for(i = 0;i < wordLine ; i++) list[i] = malloc(sizeof(char)*counter); 

你必须为wordLinecounter变量赋值

内存分配也应该在以下循环之前(外部):

  while((c = fgetc(myFile)) != EOF){ : : } 

编辑

新问题的第三个版本。 你正在读文件两次。 所以你需要在第二个循环开始之前将fseek(),rewind()转换为第一个char。

尝试:

 fseek(fp, 0, SEEK_SET); // same as rewind() rewind(fp); // same as fseek(fp, 0, SEEK_SET) 

我也怀疑你的逻辑是计算numberOfLinesmaxNumberOfChars 请检查一下

编辑

我认为你的maxNumberOfChars = 0, numberOfLines = 0是错误的尝试这样:

 maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; while((c = fgetc(myFile)) !=EOF){ if(c == '\n'){ numberOfLines++; if(maxNumberOfChars < numberOfChars) maxNumberOfChars = numberOfChars; numberOfChars=0 } numberOfChars++; } 

maxNumberOfCharsmaxNumberOfChars中的最大字符数。

也改变代码:

 malloc(sizeof(char)*(maxNumberOfChars + 1)); 

如果我是你,我会使用mmap将文件映射到私有内存,然后遍历文件,在char**数组中存储单词的开头,你可以在使用realloc增加,并替换换行符0。

这样,你把你的单词作为一个连续的块存储在内存中,你不必关心文件I / O,因为你将整个文本文件作为char*存储在内存中,而你不需要malloc一个数组数组。

有关这些函数的信息,请参阅相应的手册页,或给我发表评论:)

编辑:如果您还不知道mmap,请看看: http : //www.jimscode.ca/index.php/component/content/article/13-c/45-c-simple-mmap-example

今天大多数C程序员仍然尝试使用fopen和朋友将文件读入内存,但这完全没有必要,并引入了附加级别的复杂性。 (缓冲,增长数组,…)和mmap是一个很好的选择,将所有令人讨厌的工作移动到操作系统