c - 使用结构和数组的错误 int 值

标签 c

<分区>

你好,我有一个关于整数的问题。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
struct data
{
    char name[50];
    int grade[1];
};

int main()
{
    struct data persons[30];
    int n = 3;
    int i;

    for(i=0;i<n;i++)
    {
        printf("Type person name nr: [%d] ",i+1);
        scanf("%s",persons[i].name);
        printf("Type grade: (from 1 to 6) ");
        scanf("%d",persons[i].grade);
    }

    for(i=0;i<n;i++)
    {
        printf("Name [%d]: %s\n",i+1,persons[i].name);
        printf("Grade [%d]: %d\n",i+1,persons[i].grade);

    }

    return 0;
}

当我输入一些姓名和成绩时,输出不正确: This is my output

最佳答案

当标准 int 就足够时,您正在为单个成绩使用数组。

struct data
{
    char name[50];
    int grade;
};

然后您还需要使用 & 将 int 的引用传递给 scanf。

scanf("%d", &persons[i].grade); 

关于c - 使用结构和数组的错误 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361305/

相关文章:

c - 从C中的char数组中提取 '('和 ')'内部的字符

c - 我的奇偶排序例程有什么问题?

c - #ifdef#else - 没有选择分支

c++ - 如何使用 gnu 缩进在函数名和括号之间设置空格?

c - RET 之后的指令是否总是 CALL 之后的指令?

c - 如何让SetTimer()只发送一条WM_TIMER消息?

c - 显示十六进制的字符/数字

c - 实现我自己的 "strings"工具——GNU 字符串找到的缺失序列

c - 如何避免结构填充?

c++ - C、C++ : Shared libraries: Are single functions or complete libraries loaded into memory?