-1
struct student {
int marks[3];
int numberofsubjects[3];
};

I create : struct student student;

in my .c file,

If I try to assign using

student.marks = {99,99,99};

I do see the error: expected expression before ‘{’ token

Is there something that I am missing?

ShivaniSarin
  • 17
  • 3
  • 6
  • That struct definition isn't even compiling for me on Clang, GCC or MSVC since they all expect a struct to only have at most one flexible array as the last member of the struct – Govind Parmar Nov 16 '18 at 21:58
  • Sorry I missed out on the array size. – ShivaniSarin Nov 16 '18 at 22:11
  • Possible duplicate of [Assign multiple values to array in C](https://stackoverflow.com/questions/3535410/assign-multiple-values-to-array-in-c) – KamilCuk Nov 16 '18 at 22:16

2 Answers2

1

You can only use an initializer when you define a variable. You can't define a variable, then try to initialize it later using an initializer. You will either have to assign values to the elements of the array fields of your structure, or use an initializer at the point of definition.

struct student student = { { 99, 99, 99 },
                           { 1, 2, 3 } };

Or better yet, use designated initializers:

struct student student = { .marks = { 99, 99, 99 },
                           .numberofsubjects = { 1, 2, 3 }};
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • Can I have struct student s; defined in .h file and in my .c file as s = { .marks = { 99, 99, 99 }, .numberofsubjects = { 1, 2, 3 }}; – ShivaniSarin Nov 16 '18 at 22:44
  • @ShivaniSarin -- no. You could declare the _type_ `struct student {}` in the `.h.` file, but you can only use the initializer when you define `s` with `struct student s = { .marks = { 99, 99, 99 }, /* ... */ };`. That definition could be (and should be) in a `.c` file. – ad absurdum Nov 16 '18 at 22:48
  • I am having struct student defined in my .h file and in my .c file i have struct student s ; s = { .marks = { 99, 99, 99 },...} -> will this work?It is throiwng an errror for me – ShivaniSarin Nov 16 '18 at 23:00
  • @ShivaniSarin -- again, that will not work. You can _only_ use initializer syntax at the point of definition. You can not separate `struct student s;` from `s = /*...*/;`. The only way this works is with `struct student s = { /*...*/};` on the same line. The `struct student s` is the _definition_ of the variable `s`. If you do this: `struct student s;` then `s = /*...*/;`, you attempt initialization after definition (really you attempt an illegal assignment. With `struct student s = { '*...*/};` you define and initialize on the same line. – ad absurdum Nov 16 '18 at 23:05
  • I got your point. :) But what if I am setting this in one function and want to use it in another. in this case how do I set it as a global variable? – ShivaniSarin Nov 16 '18 at 23:11
  • It would be best by far to not use global variables. But, if you must, you can define `struct student s = { .marks = { 99, 99, 99 }, /*...*/ };` outside of `main()`. then the variable `s` will be visible to any functions in `main()`. – ad absurdum Nov 16 '18 at 23:15
0

The main thing that you are missing is that you cannot assign complete arrays; this is only allowed in the course of the definition of a variable (this is then called initialization), but its not allowed any more once the variable has been defined.

See the following initialization (which is allowed):

struct student {
    int marks[3];
    int numberofsubjects[3];
};

int main() {
    struct student s = { {1,2,3},{3,4,5}};  // in the course of variable definition; OK, this is "initialization"

    // s.marks = { 2,3,4 }; // illegal assignment of array
    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58