从字符串常量初始化的不合适类型的数组

时间:2016-10-21 05:05:09

标签: c

#include <stdio.h>
struct det{

    char fname[25];
    char lname[25];
    char sub[5];
    int marks[5];
};

int main(){

    int n, i, j;
    FILE *fp;
    fp = fopen("Student Marklist","a");
    struct det st[60] = {{"Bangla","English","Math","Science","ICT"}};

    printf ("\n Number of students : ");
    scanf ("%d", &n);

    for (i = 0; i < n; ++i){

        printf ("\n Marklist of student %d\n\n",i + 1);

        for (j = 0; j < 5; ++j){
            printf (" %s Marks : ",st[i].sub[j]);
            scanf ("%d", &st[i].marks[j]);
            fprintf (fp, " %s : %d\n", st[i].sub[j], st[i].marks[j]);

        }

    }

    fclose(fp);

    return 0;

}

“从字符串常量初始化的不合适类型数组”第12行错误。怎么办?

1 个答案:

答案 0 :(得分:0)

它期望初始化数据如{ { fname, lname, sub, marks }, {fname, lname, sub, marks }, ... }。示例:{ {"Fname1", "Lname1", "Phys", { 0,1,2,3,4} }, {"Fname2", "Lname2", "Math", { 5,6,7,8,9} }, ... }假设sub是字符串

struct det{

    char fname[25];
    char lname[25];
    char sub[5];
    int marks[5];
};

int main(int argc, char* argv[])
{
    struct det st[60] = { {"Fname1", "Lname1", "Phys", { 0, 1, 2, 3, 4 } }, { "Fname2", "Lname2", "Math", { 5,6,7,8,9} } };