【问题标题】:error: expected expression before 'student'错误:“学生”之前的预期表达式
【发布时间】:2016-10-31 00:51:45
【问题描述】:

对 c 非常陌生。我写了以下代码。

typedef struct
{
    char name[100];
    int comp, math, phys;
    int total;
} student[100];

int main(int argc, char** argv) {
int number;

do
{
    printf("Enter how many students: ");
    scanf("%d", &number);
    if(number < 0)
    {
       printf("Wrong input! \n"); 
    }
}
while(number < 0);

int i;

for(i=0; i < number; ++i)
{
    printf("Student %d's name: ", i+1);
    scanf("%s", student[i].name);
    printf("Comp: " );
    scanf("%d", &student[i].comp);
    printf("Phys: " );
    scanf("%d", &student[i].phys);
    printf("Math: " );
    scanf("%d", &student[i].math);
    &student[i].total = &student[i].comp + &student[i].math + &student[i].phys;
}

printf("s%", &student[1].name);

return (EXIT_SUCCESS);
}

我不断收到错误:在所有 scanf 行和最后一个 printf 行中,“student”之前的预期表达式。我究竟做错了什么?对 C 非常陌生,所以任何帮助都会很棒。

【问题讨论】:

  • 您正试图将student 用作数组object。但是您将其声明为数组type。类型和对象是两个完全不同的东西。
  • 在以&amp;student[i].total =开头的行中,取出所有&amp;

标签: c struct


【解决方案1】:

struct
{
    char name[100];
    int comp, math, phys;
    int total;
} student[100];

如果你想结合定义和标识符。你应该知道 student 不是一个类型,它是一个没有名字的结构数组。您想要完成的任务还有其他选择。例如:

typedef struct student
{
    char name[100];
    int comp, math, phys;
    int total;
} student;

student students[100];

【讨论】:

    【解决方案2】:

    删除typedef 关键字。您想要创建一个包含 100 个学生对象的数组,而不是代表一个包含 100 个学生的数组的类型名称。

    希望这对您将来有所帮助:

    // `Type var` is a shorter way to write `struct tag var`
    // `Type` is just an alias (another name) for `struct tag`
    typedef struct tag {
        int x;
    } Type;
    
    // `Type100 arr` is a shorter way to write `struct tag100 arr[100]`
    // `Type100` is just an alias for `struct tag100[100]`
    // No, you can't do `struct tag100[100] arr`; `Type100 arr` gets around this restriction
    typedef struct tag100 {
        int x;
    } Type100[100];
    
    // `var` contains a single value of type `struct tagX`
    struct tagX {
        int x;
    } var;
    
    // `arr` is an array of 100 values of type `struct tagX100`
    struct tagX100 {
        int x;
    } arr[100];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 2023-04-11
      • 2014-04-20
      • 2015-03-28
      • 2021-02-19
      • 2019-09-23
      • 2014-11-30
      相关资源
      最近更新 更多