Reputation: 1
Here is the current code. For averaging gradesa from 3 students. Underneath the code are the errors im facing. Was able to work them down from 25 to 4.
Visual studio is saying i have an unidentified expression E0029, E0020, C2065, C2109
dont believe im using pointers as we were told not use them. Read the error codes and looked them but cant determine where im falling short.
Thank you in advanced.
#include <stdio.h>
#define N 5
struct student {
char firstName[50];
int roll;
float marks[3];
}s[N];
typedef struct {
char firstName[50];
int roll;
float marks[3];
}student_t;
int main() {
int i;
double total = 0;
double marks_avg ;
student_t personA, personB, SMCstudent[N];
SMCstudent->marks[0] = 99;
personA= { "Daniel",10 {100,98,90}
};
printf("Enter information of student:\n");
//storing
for (i = 0;i < N;++i); {
s[i].roll = i + 1;
printf("\nFor all number %d,\n", s[i].roll);
printf("Enter first name:");
scanf_s("%s", &s[i].marks);
}
printf("Enter 3 marks");
for (int j = 0; j < 3; j++) {
printf("enter grade\n");
scanf_s("%f", s[i].marks[j]);
scanf_s("%f", & array[i]);
}
for (i = 0; i < N; i++) {
total += s[i].marks[2];
}
marks_avg = total / N;
printf("average grade for 5 students is %f\n\n", marks_avg);
printf("displaying information:\n\n");
return 0;
}
Upvotes: 0
Views: 122
Reputation: 64672
@RetiredNinja claims that you can only do a structure assignment during initialization.
This is simply not true.
You need to specify the type name as well, but this code is valid:
personA = (student_t){ "Daniel", 10, {100.f,98.f,90.f} };
This is assigning a "compound literal" to a structure.
(l-value personA
is the structure, and the right side, (student_t){...}
is the compound literal)
Example, working code:
#include <stdio.h>
typedef struct {
char firstName[50];
int roll;
float marks[3];
}student_t;
int main(void) {
student_t personA;
personA= (student_t){ "Daniel",10, {100.f,98.f,90.f} };
printf("%s(%d): %.2f %.2f %.2f", personA.firstName, personA.roll,
personA.marks[0], personA.marks[1], personA.marks[2]);
return 0;
}
Upvotes: 2