【问题标题】:error: expression must have struct or union type in c错误:表达式在 c 中必须具有结构或联合类型
【发布时间】:2014-10-09 03:47:25
【问题描述】:

我一直在研究一种代码来存储学生的姓名和成绩,然后在输入学生姓名时回忆起成绩。

这是我的代码:

#include <stdio.h>

#define N 10
#define M 2

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

int main()
{
    int i;
    int j;
    struct a A[N][M];

    for(i=0;i<N;i++)
    {
         printf("Please Enter Students' Names:/n");
            scanf("%s", &A[i].name);
    }
    for(j=0;j<M;j++)
    {
        printf("Please Enter Students' Grades:/n");
            scanf("%d", &A[j].grade);
    }
    printf("Which Student's Grades Would You Like To View?/n");
        if(scanf("%s", *A[i].name))
        {
            printf("Their Grade Is:%d/n", *A[j].grade);
        }
    return 0;
}

我遇到了这些错误:

hw2problem2.c(21): error: expression must have struct or union type
                        scanf("%s", &A[i].name);
                                     ^
hw2problem2.c(26): error: expression must have struct or union type
                        scanf("%d", &A[j].grade);
                                     ^
hw2problem2.c(29): error: expression must have struct or union type
                if(scanf("%s", *A[i].name))
                                ^
hw2problem2.c(31): error: expression must have struct or union type
                        printf("Their Grade Is:%d/n", *A[j].grade);
                                                       ^
compilation aborted for hw2problem2.c (code 2)

对于错误或一般程序的任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: c arrays struct


    【解决方案1】:

    您将struct a A 定义为二维数组,并且仅指定 scanf("%s", &amp;A[i].name);scanf("%d", &amp;A[j].grade); 中的一维。

    您还有其他一些问题,例如 scanf("%s", &amp;A[i].name);... &amp; 是不必要的。

    【讨论】:

      【解决方案2】:

      你的程序应该是这样的

      for(i=0;i<N;i++)
      {
         for(j=0;j<M;j++)
         {
          printf("Please Enter Students' Grades:/n");
              scanf("%d", &A[i][j].grade);
          printf("Please Enter Students' Names:/n");
              scanf("%s", &A[i][j].name);
      
          }
      }
      

      因为A[i] 的类型是struct a*,而不是struct a。应该是 A[i][j]

      从逻辑上讲,您的数组应该是一维的。因此,循环应该是:

      struct a A[N];
      
      for(i=0;i<N;i++)
      {
           printf("Please Enter Students' Names:/n");
              scanf("%s", &A[i].name);
      }
      for(j=0;j<N;j++)
      {
          printf("Please Enter Students' Grades:/n");
              scanf("%d", &A[j].grade);
      }
      

      如果它是主题明智的,那么它应该是 2D 并使用如图所示的嵌套循环。

      【讨论】:

        【解决方案3】:

        无需使用2D 数组。试试这个..

        #include <stdio.h>
        #define N 3
        
        struct a
        {
         char name[50];
         int grade;
        };
        
        int main()
        {
        int i;
        struct a A[N];
            char sname[50];
        for(i=0;i<N;i++)
        {
             printf("Please Enter Students' Names:/n");
                scanf("%s", A[i].name);
        }
        for(i=0;i<N;i++)
        {
            printf("Please Enter Students' Grades:/n");
                scanf("%d",&A[i].grade);
        }
        
             printf("Which Student's Grades Would You Like To View?/n\n");
             printf("Enter the name...\n");
             scanf("%s",sname);
          for(i=0;i<N;i++)
          {
            if(strcmp(A[i].name,sname)==0)
            {
            printf("%s grade is %d\n",A[i].name,A[i].grade);
            break;
            }
            else
            {
            if(i==N-1)
            printf("No such a name in your list...\n");
            }
          }
         return 0;
         }
        

        【讨论】:

        • 测试用例:A[i].name 超过 10 个字符,sname 使用相同搜索,将导致 Stack Smashing,因为 sname 的大小为 10,您将扫描超过那个值,See Here。还有unused variable j.
        • 一般来说 scanf 和 %s 一起使用不是一个好主意,容易导致缓冲区溢出。最好使用 fgets/sscanf
        猜你喜欢
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        相关资源
        最近更新 更多