【问题标题】:using realloc won't store values in array after exiting the funcion退出函数后,使用 realloc 不会将值存储在数组中
【发布时间】:2012-01-12 16:24:46
【问题描述】:

将数据库中的值放入数组中并将值发送到函数本身内的addReader后,值已成功存储,但是当返回主时输入的值消失了。

给定以下代码:使用realloc 创建动态数组

reader* readerBuilder(reader *rdr, int *readNum){       //building all the readers
    FILE *read;
    int i=1;
    char *str;
    read = fopen("Readers.txt","r");
    checkFile(read);
    str = readFromFile(read);
    while(str != NULL)
    {
        rdr[i-1] = *cutToRdr(str);
        str = readFromFile(read);
        if(str != NULL){
            i++;
            rdr = (reader*)realloc(rdr,sizeof(reader)*i);
            checkreader(rdr);
        }
    }
    fclose(read);
    *readNum = i;
    return rdr;
}

给定调用函数:

reader* addReader(reader *rdr, int *readNum){       //adding a reader from the user
    char string[1000];
    rdr = (reader*)realloc(rdr,sizeof(reader)*(*readNum+1));// build new struct array ( bigger), with the old values
    checkreader(rdr);// check if can get memory
    printf("please enter the I.D. of the student you want to add:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].id=cutToStr(string);// put id in struct
    printf("please add the reader's first name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].first_name=cutToStr(string);// put first name in struct
    printf("please add the reader's last name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].last_name=cutToStr(string);// put last name in struct
    printf("please add the reader's address:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].address=cutToStr(string);// put adress in struct
    printf("please add the reader's phone:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].phone=cutToStr(string);// put phone in struct
    rdr[*readNum].numToTake = 5;// change value of numbers to tke to 5
    *readNum = *readNum + 1;// rise the number of the readers
    return rdr;// return the new structs array
}

鉴于 main 中的函数调用:

rdr=addReader(rdr,readNum);

阅读器结构的定义:

typedef struct reader{
    int numToTake;
    char *first_name, *last_name, *address, *phone, *id;
}reader;

我做错了什么? 问候,大卫

编辑!

还是不行。 新版代码:

void addReader(reader **rdr, int *readNum){     //adding a reader from the user
    char string[1000];
    rdr = (reader**)realloc(rdr,sizeof(reader*)*(*readNum+1));// build new struct array ( bigger), with the old values
    checkreader(*rdr);// check if can get memory
    printf("please enter the I.D. of the student you want to add:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->id=cutToStr(string);// put id in struct
    printf("please add the reader's first name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->first_name=cutToStr(string);// put first name in struct
    printf("please add the reader's last name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->last_name=cutToStr(string);// put last name in struct
    printf("please add the reader's address:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->address=cutToStr(string);// put adress in struct
    printf("please add the reader's phone:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->phone=cutToStr(string);// put phone in struct
    rdr[*readNum]->numToTake = 5;// change value of numbers to tke to 5
    *readNum = *readNum + 1;// rise the number of the readers
    //return rdr;// return the new structs array

}

main 中的新调用:

addReader(&rdr,readNum);

【问题讨论】:

  • cutToStr 是做什么的?如果它返回一个指向同一个字符串的指针,那么这个字符串将被一次又一次地覆盖。 readerBuilder 甚至被使用了吗?以及rdr 是如何传递给addReader 的?
  • "cutstr" 获取静态字符数组并返回指向动态分配的字符 * 的指针(用于结构的单元格)。

标签: c realloc


【解决方案1】:

你应该通过**rdr

reader* addReader(reader **rdr, int *readNum)
                         ^^

此外,这会在您realloc 时添加一个新的间接级别。

使用您现在拥有的代码,您 realloc 在指针的副本而不是实际指针上。

【讨论】:

  • 你能解释一下为什么是**而不是一个*吗?
  • @zahidavid:在 c/c++ 中搜索按值传递与按引用传递。
  • 我明白了,谢谢。不使用 ** (pointer to pointer) 有没有其他方法可以做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多