C 警告“格式 '%d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int"; [英] C Warning "format '%d' expects argument of type 'int *', but argument 2 has type 'int"

查看:93
本文介绍了C 警告“格式 '%d' 需要类型为 'int *' 的参数,但参数 2 的类型为 '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);

推荐答案

  1. struct Students中,int points[10];应该是int points;int missings[10]]; 应该是 int missings;

  1. In struct students, int points[10]; should be int points;, int absences[10]; should be int 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆