Code:
#include <stdio.h>
void grade(struct student_info *);
int main()
{
struct student_info
{
int examno;
char name[25];
int marks;
char grade;
};
struct student_info student[50], *p;
int num, i;
printf( "Program of structure with pointer.\n\n") ;
printf( "How many students ? :" );
scanf( "%d", &num );
for (i=0; i<num; i++ )
{
printf( "Enter information of student %d\n", i+1 );
printf( "Exam Number :" );
scanf( "%d%*c", &student[i].examno );
printf( "Name :" );
scanf( "%[^\n]", student[i].name );
printf( "Total marks obtained of 100 :" );
scanf( "%d", &student[i].marks );
}
for ( p=student; p < student+num; p++ )
{
grade(p);
}
printf( "\n" );
printf( "Exam Number Name Marks obtained in class Grade\n\n" );
for ( p=student; p < student + num; p++ )
{
printf( "%7d %-25s %9d %8c\n", p->examno, p->name,p->marks, p->grade );
}
return 0;
}
void grade(struct student_info *p)
{
if (p->marks < 50)
p->grade = 'F';
else
{ if (p->marks < 60)
p->grade = 'D';
else
{
if (p->marks < 70)
p->grade = 'C';
else
if (p->marks < 80 )
p->grade = 'B';
else
p->grade = 'A';
}
}
}
Okay, the code above does not compiles (9 errors 1 warning).
And I solved it by moving the structures outside main()
Now my question is,
Why the structures must be declared outside?
is it a must to declare structure outside main?
I hope someone can explain it for me.
Thanks.
(BTW, most of the code isn't mine, it's given in my school textbook)