结构中的变量接收未声明的错误(此函数中首次使用)



这是我的一小段代码,似乎是问题的一部分。

struct File {
    int *childrenDir[10];
    int childDirIndex;
};
struct File *File_create() {
    struct File *file = malloc(sizeof(struct File));
    assert(file != NULL);
    file->childDirIndex = 0;
    return file;
}
struct File currentDir[20];
int curDirIndex = 0;
root = *File_create();
currentDir[curDirIndex] = root;
void overhead() {
    currentDir[curDirIndex].childrenDir[childDirIndex] = 0;
    currentDir[curDirIndex].childDirIndex++;
    fileID++;
}

当我从其他函数调用overhead()时,编译器会向我抛出一个错误

‘childDirIndex’ undeclared (first use in this function)

当我认为每当调用File_create()时都会声明childDirIndex时,我不太清楚为什么它会给我这个错误。

void overhead() {
    currentDir[curDirIndex].childrenDir[childDirIndex] = 0;
//                                      ^ This is the trouble!
    currentDir[curDirIndex].childDirIndex++;
    fileID++;
}

也许你的意思是:

void overhead() {
    currentDir[curDirIndex].childrenDir[currentDir[curDirIndex].childDirIndex++] = 0;
    fileID++;
}

或者,更理智、更容易理解:

void overhead() {
    struct File *fp = &currentDir[curDirIndex];
    fp->childrenDir[fp->childDirIndex++] = 0;
    fileID++;
}

最新更新