C 创建一个动态字符串数组,该数组将在填充时进行缩放

C 创建一个动态字符串数组,该数组将在填充时进行缩放,c,arrays,string,dynamic-arrays,realloc,C,Arrays,String,Dynamic Arrays,Realloc,好的,我正在读取一个txt文件,并将每一项按新行存储到一个字符串数组中。唯一的问题是,每当数组接近填满时,我需要将其大小调整5 输入看起来像这样,只是大了很多 CSE 1104 CSE 1105 CSE 1310 CSE 2320 IE 2308 PHYS 1443 MATH 1426 这是我的密码 void printFileError(char *s) { printf("unable to open %s\n", s); exit(1); } void reallocT

好的,我正在读取一个txt文件,并将每一项按新行存储到一个字符串数组中。唯一的问题是,每当数组接近填满时,我需要将其大小调整5

输入看起来像这样,只是大了很多

CSE 1104
CSE 1105
CSE 1310
CSE 2320
IE 2308
PHYS 1443
MATH 1426
这是我的密码

void printFileError(char *s) {
    printf("unable to open %s\n", s);
    exit(1);
}

void reallocTaken(char **taken, int *size, int newSize) {
    (*size) = newSize;
    taken = realloc(taken, (*size) * sizeof(char *));
    printf("Realocateding---- \n");
}

int main(int argc, char *argv[]) {
    char **coursesTaken;
    int coursesTakenSize = 5;

    int i;

    char *filePlan, *fileMyCourses;
    FILE *fp;

    /* Open the file of completed courses */
    if((fp = fopen(argv[2], "r")) != NULL) {
        char buffer[255];
        int lineCount = 0;

        coursesTaken = malloc(sizeof(char *) * coursesTakenSize);

        while(fgets(buffer, sizeof(buffer), fp) != NULL) {
            char token[] = "\n";
            char *split = strtok(buffer, token);

            while(split != NULL) {

                /* Check if array is full */
                if((lineCount + 1) == coursesTakenSize) {
                    printf("Needs to allocate \n");
                    /* Realoc Memory */
                    reallocTaken(coursesTaken, &coursesTakenSize, coursesTakenSize + 5);
                }

                coursesTaken[lineCount] = malloc((sizeof(split) + 1) * sizeof(char));
                strcpy(coursesTaken[lineCount], split);

                printf("%s\n", split);

                lineCount++;
                split = strtok(NULL, token);
            }
        }

        /* Cut out exessive memory */
        reallocTaken(coursesTaken, &coursesTakenSize, lineCount);

        printf("Line Count: %d\n", lineCount);
        printf("Size: %d\n", coursesTakenSize);
        printf("Final Size: %lu\n", sizeof(coursesTaken));
        fclose(fp);
    } else {
        printFileError(argv[2]);
    }

    for(i = 0; i < coursesTakenSize; i++) {
        printf("%d:\t%s\n", i+1, coursesTaken[i]);
    }

    return 0;
}
这种情况发生在这里

CSE 1104
CSE 1105
CSE 1310
CSE 2320
Needs to allocate 
Realocateding---- 
IE 2308
PHYS 1443
MATH 1426
MATH 1302
MATH 1323
Needs to allocate 
我注意到通过注释
coursesTaken[lineCount]=malloc((sizeof(split)+1)*sizeof(char))
程序完全运行,直到每次打印结束


我真的不太确定这里发生了什么。

您将
sizeof
strlen混为一谈,前者可以给出单个元素、结构或大小已知的数组的大小,后者给出以null结尾的字符串的长度

这一行
coursesTaken[lineCount]=malloc((sizeof(split)+1)*sizeof(char))
确实是罪魁祸首。在下一行中,您将
split
复制到该行中,您需要它可以包含split中的所有字符以及终止的null

但由于
split
char*
sizeof(split)
指针的大小(例如,32位系统为4,64位系统为8)

你应该写这行:

coursesTaken[lineCount] = malloc((strlen(split) + 1) * sizeof(char));
你想要:

void reallocTaken(char ***taken, int *size, int newSize) {
    (*size) = newSize;
    *taken = realloc(*taken, (*size) * sizeof(char *));
    printf("Realocateding---- \n");
}
可以这么说:

    reallocTaken(&coursesTaken, &coursesTakenSize, coursesTakenSize + 5);
在原始代码中,您正在丢失指向重新分配的空间的指针,方法是将其分配给
take
,而不是
*take
,并在该点继续使用指向原始释放空间的指针。该原则与您正确应用于
courseTakenSize
的原则相同


当您再次尝试重新分配原始指针时,您报告的错误如下。

您正在搜索
realloc()
@iharob是的,我正在使用它,我是否也需要使用它来分配每个字符串??
    reallocTaken(&coursesTaken, &coursesTakenSize, coursesTakenSize + 5);