C 警告“格式 '%d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int"; [英] C Warning "format '%d' expects argument of type 'int *', but argument 2 has type 'int"
问题描述
我想用一个字符串和一些数字填充这些数组,但似乎无法弄清楚为什么我不能.
I want to fill up these arrays with a string and some numbers, but can't really seem to figure out why I cannot.
#include <stdio.h>
struct students{
char name[30];
int points[10];
int absences[10];
};
int main()
{
int i, n;
printf("Declare the number of students: ");
scanf("%d", &n);
struct students stud[n];
for (i = 0; i < n; i++) {
printf("Name: ");
scanf("%s", &stud[i].name);
printf("Points: ");
scanf("%d", &stud[i].points);
printf("Absences: ");
scanf("%d", &stud[i].absences);
}
for( i = 0; i < n; i++)
{
printf("%s\n", stud[i].name);
printf("%d\n", stud[i].points);
printf("%d\n", stud[i].absences);
}
}
这是我收到的警告:
警告:格式 '%s' 需要类型为 'char ' 的参数,但参数 2 的类型为 'char ()[30]' [-Wformat=]
warning: format '%s' expects argument of type 'char ', but argument 2 has type 'char ()[30]' [-Wformat=]
scanf("%s", &stud[i].name);
feladat1.c:21:15: 警告:格式 '%d' 需要类型为 'int ' 的参数,但参数 2 的类型为 'int ()[10]' [-Wformat=]
feladat1.c:21:15: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[10]' [-Wformat=]
scanf("%d", &stud[i].points);
feladat1.c:23:15: 警告:格式 '%d' 需要类型为 'int ' 的参数,但参数 2 的类型为 'int ()[10]' [-Wformat=]
feladat1.c:23:15: warning: format '%d' expects argument of type 'int ', but argument 2 has type 'int ()[10]' [-Wformat=]
scanf("%d", &stud[i].absences);
feladat1.c:30:16: 警告:格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'int *' [-Wformat=]
feladat1.c:30:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
printf("%d\n", stud[i].points);
feladat1.c:31:16: 警告:格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'int *' [-Wformat=]
feladat1.c:31:16: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
printf("%d\n", stud[i].absences);
推荐答案
在
struct Students
中,int points[10];
应该是int points;
,int missings[10]];
应该是int missings;
In
struct students
,int points[10];
should beint points;
,int absences[10];
should beint absences;
行 scanf("%s", &stud[i].name);
应该是 scanf("%s",stud[i].name);
以下 code
可以工作:
#include <stdio.h>
#include <stdlib.h>
struct students{
char name[30];
int points;
int absences;
};
int main()
{
int i, n;
printf("Declare the number of students: ");
scanf("%d", &n);
struct students *stud = malloc(sizeof(struct students) * n);
for (i = 0; i < n; i++) {
printf("Name: ");
scanf("%s", stud[i].name);
printf("Points: ");
scanf("%d", &stud[i].points);
printf("Absences: ");
scanf("%d", &stud[i].absences);
}
for( i = 0; i < n; i++)
{
printf("%s\n", stud[i].name);
printf("%d\n", stud[i].points);
printf("%d\n", stud[i].absences);
}
return 0;
}
这篇关于C 警告“格式 '%d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!