c - 采用不同数据类型的多个元素的数组

标签 c arrays typedef

我想获取包含学生姓名、卷号的类(class)信息。并打印出来 我使用了结构,但代码不起作用

#include<stdio.h>
#include <string.h>
int size,i,j;

struct Student
{
    char name[50];
    int number;
};

typedef struct Student info1;
info1 print[100];

int main()
{
     printf("Size of class : ");
     scanf("%d",&size);


    for(j=0;j<(20);)
    {
        for(i=1;i<=size;i++)
        {

            printf("%d.Name:",i);
            scanf("%s",&info1.name);// i get an error here that an exp is expected
            print[j]=info1.name; // i get an error here that an exp is expected
            j++;
            printf("Rollno:");
            scanf("%d", &info1.number);// i get an error here that an exp is expected
            print[j]=info1.number;// i get an error here that an exp is expected

        }

    }   
    for (j = 0;j <(20);)
    {
        printf("%s Name", print[j]);
        j++;
        printf("%d Rollno", print[j]);
    }

    return 0;  
}

调试时出现以下错误:

expected exp before info1

最佳答案

在您的代码中,对于所有用法,例如

 scanf("%s",&info1.name);
             ^^^^^

是错误的,因为 info 是数据类型的别名,而不是变量。

您已经定义了该类型的变量,print,请使用它。

关于c - 采用不同数据类型的多个元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724938/

相关文章:

c - 这个双重类型定义会导致访问冲突吗?

c - 为结构体定义 typedef 2 次

c++ - c/c++ 中数组元素的别名

C for循环继续递增

比较所有数组元素——C算法

javascript - 将数组分组为月和日

c# - 如何查找镜像重复项的所有实例?

c - 当函数返回时,临时存储在 C 中如何工作?

c - 哪种方法对于初始化 wchar_t 字符串是正确的?

c - 使用稍后声明的 C 结构