ghy19980814
2017-06-05 06:54
采纳率: 83.3%
浏览 875
已采纳

C语言结构体指针问题求修改

#include
int main(void)
{
struct student {
int num;
char name[19];
double score;
};
int i,k,N;
scanf("%d",&N);
for(i=0;i {
struct student s[3];
struct student *p;
for(k=0;k scanf("%d%s%lf",&s[k].num,&s[k].name,&s[k].score);
p = &s[k];
}
for(k=0;k printf("%d\t%-20s%lf\n",s[k].num,s[k].name,s[k].score);
}
for(k=0;k printf("%d\t%-20s%lf\n",p->num,p->name,p->score);
}
}
return 0;
}
编译器显示:Your Code: In function 'main':
Your Code:17:19: warning: format '%s' expects argument of type 'char ', but argument 3 has type 'char ()[19]' [-Wformat=]
scanf("%d%s%lf",&s[k].num,&s[k].name,&s[k].score);
^
0:编译失败,请检查源代码!

请问如何解决这个问题?谢谢各位好心人!急急急!!!


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 战在春秋 2017-06-05 07:09
    已采纳
     scanf("%d%s%lf",&s[k].num,&s[k].name,&s[k].score);
    

    应为:

     scanf("%d%s%lf",&s[k].num,s[k].name,&s[k].score);
    

    看一下结构体中name的定义:

     char name[19]; //数组名name就是数组的首地址,所以用%s读入时,不用再加取地址运算符&。
    

    用心回答每个问题,如果有帮助,请采纳答案好吗,谢谢~~~

    点赞 打赏 评论
  • ghy19980814 2017-06-05 07:04

    提问显示好像有些问题
    #include
    int main(void)
    {
    struct student {
    int num;
    char name[19];
    double score;
    };
    int i,k,N;
    scanf("%d",&N);
    for(i=0;i {
    struct student s[3];
    struct student *p;
    for(k=0;k scanf("%d%s%lf",&s[k].num,&s[k].name,&s[k].score);
    p = &s[k];
    }
    for(k=0;k printf("%d\t%-20s%lf\n",s[k].num,s[k].name,s[k].score);
    }
    for(k=0;k printf("%d\t%-20s%lf\n",p->num,p->name,p->score);
    }
    }
    return 0;
    }
    编译器显示:Your Code: In function 'main':
    Your Code:17:19: warning: format '%s' expects argument of type 'char ', but argument 3 has type 'char ()[19]' [-Wformat=]
    scanf("%d%s%lf",&s[k].num,&s[k].name,&s[k].score);
    ^
    0:编译失败,请检查源代码!

    请问如何解决这个问题?谢谢各位好心人!急急急!!!

    点赞 打赏 评论
  • ghy19980814 2017-06-05 07:05

    for(i=0;i小于N;i++)
    for(k=0;k小于3;k++)

    点赞 打赏 评论

相关推荐