C 警告 "format ' %d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int"

标签 c warnings

我想用一个字符串和一些数字填充这些数组,但似乎无法真正弄清楚为什么我不能。

#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=]

     scanf("%s", &stud[i].name);

feladat1.c:21:15:警告:格式“%d”需要“int ”类型的参数,但参数 2 的类型为“int ()[10]”[-Wformat= ]

     scanf("%d", &stud[i].points);

feladat1.c:23:15:警告:格式“%d”需要“int ”类型的参数,但参数 2 的类型为“int ()[10]”[-Wformat= ]

     scanf("%d", &stud[i].absences);

feladat1.c:30:16:警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“int *”[-Wformat=]

     printf("%d\n", stud[i].points);

feladat1.c:31:16:警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“int *”[-Wformat=]

     printf("%d\n", stud[i].absences);

最佳答案

  1. struct Students中,int point[10];应为int point;int Absences[10 ]; 应该是 int 缺勤;

  2. scanf("%s", &stud[i].name); 应该是 scanf("%s", Stud[i].name);

以下代码可以工作:

#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 警告 "format ' %d' 需要类型为 'int *' 的参数,但参数 2 的类型为 'int",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071231/

相关文章:

xcode - "cacheParamsComputed"在 Swift 4 中意味着什么?

C 编译警告 : passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]

c - 多个定义在 gcc 或 clang 中都不会给出错误或警告

c - 为什么我在这段代码中有一个无限循环

objective-c - 用于从 Microsoft PowerPoint 演示文稿中提取数据的 Cocoa API

c - 为什么if else或嵌套if else在C中被称为单语句

c - C 中的段错误套接字

c++ - 如何在CodeBlocks中使用PdCurses的CDK库?

reactjs - react 警告渲染()

c++ - 我怎样才能打开(字面上)所有 GCC 的警告?