0

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;
}
1
  • You can only do something like personA= { "Daniel",10 {100,98,90} }; when initializing the variable, not afterward. You're also missing a comma. scanf_s("%f", & array[i]); fails because there is no variable named array in this code. Why do you have two identical structs defined? Commented Dec 2, 2022 at 21:04

1 Answer 1

2

@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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@abelenky Nice, I learned something new. You didn't mention the missing comma or the missing array variable though.
@abelenky you may want to add that it’s a good idea to use designated initializers if assigning a variable this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.