2016-10-31 5 views
0

очень новое к 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. Что я делаю не так? Очень новичок в C, поэтому любая помощь будет отличной.

+0

Вы пытаетесь использовать 'student' как массив * объект *. Но вы объявили его как массив * type *. Типы и объекты - это две совершенно разные вещи. – AnT

+0

В строке, начинающейся с '& student [i] .total =', вытащите все '&' –

ответ

4

ли

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

Если вы хотите совместить определение и идентификатор. Вы должны знать, что студент не является типом, это массив структур без имени. Существуют альтернативы тому, что вы хотите выполнить. Например:

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

student students[100]; 
0

Оставьте ключевое слово 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]; 
Смежные вопросы