2017-01-18 64 views
1

我想获取包含学生姓名,卷号,并打印 我使用结构,但代码不工作一个数组,其中包含不同数据类型的多个元素

#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; 
} 

在调试时我收到以下错误:

前INFO1

回答

1

在你的代码预期EXP,对于所有使用像

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

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

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

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

这是罚款和平常

typedef struct Student info1; 
info1 print[100]; 

这强烈暗示我,你不知道你在做什么。 info1现在是结构Student的别名。而不是一个奇怪的名字。然后,您将创建一个名为print的缓冲区,其容量为一百。

我们可以轻松修复您的编译时错误。您可以指定一个strudnet

struct Student astudent; 

print[i] = astudent; 

,你必须初始化他第一

scanf("%s %d", astudent.name, &astudent.number); 

,但你不能分配给类型“INFO1”。对于一系列学生来说,“印刷”是一个非常糟糕的选择。至少使它成为“打印列表”。

相关问题