Code:
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[20];
char family[30];
char namedan[30];
float mark;
};
void print (struct student *, int);
int marks (struct student *, int);
int main( void ){
int i;
char ch;
struct student *p;
p = malloc (sizeof *p);
for(i=1;;i++){
printf("\nenter %domin student's name:\n",i);
scanf("%s",p[i-1].name); /* also: fgets would work */
printf("enter %domin student's family:\n",i);
scanf("%s",p[i-1].family);
printf("enter %domin student's namedan:\n",i);
scanf("%s",p[i-1].namedan);
printf("enter %domin student's average:\n",i);
scanf("%f",&p[i-1].mark);
printf("do you want to enter another student's information?(y or n): ");
ch=getchar();
if (ch!='y') break;
else
{
struct student *q = realloc( p, (i+1) * sizeof *p );
if( q != NULL )
p = q;
else
{
printf( "out of memory\n" );
return EXIT_FAILURE;
}
}
}
print(p,i);
printf ("average of student's marks is: %f",marks(p,i));
getchar();
return 0;
}
void print (struct student *p, int s){
int i;
for (i=1;i<=s;i++){
printf("\n%s\t%s\t%s\t%f\n",(*p).name,(*p).family,(*p).namedan,(*p).mark);
p++;
}
}
int marks (struct student *p, int s){
int i;
float avg,sum=0;
for (i=1;i<=s;i++){
sum=sum+(*p).mark;
p++;
}
avg=sum/s;
return avg;
}
That should get you going in the right direction.
You seem to be compiling as C++:
Code:
void print (student *, int);
int marks (student *, int);
int main(){
int i;
char ch;
p=(student *) malloc (sizeof(student));
Instead of C:
Code:
void print (struct student *, int);
int marks (struct student *, int);
int main( void ){
int i;
char ch;
struct student *p;
p = malloc (sizeof *p);
Quzah.