如何在C中为char **动态分配内存 [英] How to dynamically allocate memory for char** in C

查看:904
本文介绍了如何在C中为char **动态分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I go about dynamically allocating memory to char** list in this function?

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

Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length.

我必须用C弦做其他事情,但是我应该会满意的.

I have to do other stuff with the C-strings but that stuff I should be fine with.

谢谢!

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 < wordLine ; i++)
        list[i] = malloc(sizeof(char)*maxNumberOfChars);


    while((c = fgetc(myFile)) != EOF)
    {
        if(c == '\n' && counter > 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<number_of_row; i++) 
  list[i] = malloc(sizeof(char)*number_of_col);  

此外,如果要动态分配内存.您将在完成工作后将其释放:

Additionally, if you are allocating memory dynamically. you are to free it as work done:

for(i=0;i<number_of_row; i++) 
  free(list[i] );
free(list);  

编辑

在您修改的问题中:

 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变量

you have to assign value to wordLine and counter variable

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

Also memory allocation should be before the following loop(outside):

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

编辑:

重新输入问题的第三个版本.您正在读取文件两次.因此,您需要 fseek(),rewind() > 到第二个循环开始之前的第一个字符.

New your third version of question. You are reading file two times. So you need to fseek(), rewind() to first char before second loop starts.

尝试:

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

我也怀疑您的逻辑来计算numberOfLinesmaxNumberOfChars.请同时检查

also I have doubt in your logic to calculate numberOfLines and maxNumberOfChars. please check that also

编辑

我认为您对maxNumberOfChars = 0, numberOfLines = 0的计算是错误的,如下所示:

I think your calculation for maxNumberOfChars = 0, numberOfLines = 0 is wrong try like this:

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

maxNumberOfChars是一行中最大字符数.

maxNumberOfChars is max number of chars in a line.

还要更改代码:

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

这篇关于如何在C中为char **动态分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆