从显示错误的文件输入指针数组

Input to an array of pointers from a file showing error

我遇到 "subscripted value is not an array, pointer, or vector" empF[i]
这个错误 这是我的代码

employee empF[100];
int i=0;
void test(){
    FILE *fp;
    employee empF;
    fp=fopen("employee.csv","r");
    employee temp;
    char x[100];
    while(fgets(x,100,fp)!=NULL){
        removeCommas(x);
        sscanf(x,"%d %s %ld %s %d",&(temp.employee_id),temp.employee_name,&(temp.phno),temp.shift,&(temp.area_code));
        empF[i].employee_id=temp.employee_id;
        empF[i].employee_name=temp.employee_name;
        empF[i].phno=temp.phno;
        empF[i].shift=temp.shift;
        empF[i].area_code=temp.area_code;
        i+=1;
    }
    fclose(fp);
}

这是员工结构

typedef struct employee
{
    int employee_id;
    char employee_name[20];
    long int  phno;
    char shift[10];
    int area_code;
}employee;

如果我不使用结构数组,似乎 运行 就好了。我到底错过了什么?

我猜你忘了删除

employee empF;

当您从一名员工切换到多名员工时。此声明覆盖先前的数组声明。

(此外,您实际上并不需要 temp 员工;您可以直接扫描到 empF[i],但您可能有自己的理由:))

您在函数中重新定义了变量名empF,并声明为employee(非数组)类型。

void test(){
    FILE* fp;
    employee empF; /* <-- here */
    /* ... */
}

因此,当您引用 empF 时,您引用的是函数内部的非数组局部变量。