0

I have the following code:

struct StudentStruct {
  char name[32];
  float score;
};

and it is initialized this way:

  StudentStruct students[1000];
  for (size_t i = 1; i < sizeof(students) / sizeof(students[0]) - 5; ++i) {
    Ops[i] = (StudentStruct){"Empty", 0.0};
  }
  students[  0] = (StudentStruct){"Alex",    78.9};
  // There will always be a huge gap, which could prevent some
  // possible syntax to work
  students[995] = (StudentStruct){"Bob",     3.14};
  students[996] = (StudentStruct){"Charlie", 2.71};
  students[997] = (StudentStruct){"David",   99.9};
  students[998] = (StudentStruct){"Eugene",  0   };
  students[999] = (StudentStruct){"Frank",   -2.2};
  // This is just an example, in reality I would need to manually define hundreds of them.

This works well until I enabled -Wpedantic which complains that warning: ISO C++ forbids compound-literals [-Wpedantic]. I am wondering if there is a ISO C++-compliant way that does something very similar.

I am aware of the following:

  1. brace initialization: but after playing for a while it seems to me that it can't fit into my case.
  2. change struct to class and prepare a constructor: this should work, but I would prefer keeping struct if possible because the code may potentially be called in other languages, have a simple memory layout makes me feel a little bit better.

Any idea?

EDIT:

  • Some answers point to this:
students[0] = { "Alex",    78.9 };

I am not sure why, it doesn't work with my g++, the error is: error: no match for ‘operator=’ (operand types are ‘StudentStruct’ and ‘<brace-enclosed initializer list>’)

  • I am using g++ 10.2.1 and using the method in this post, the default C++ standard is C++14.
D.J. Elkind
  • 367
  • 2
  • 8

1 Answers1

1

Just remove the parens around the type name. Why would you need it?

students[  0] = StudentStruct{"Alex",    78.9};

Or you can remove the type name completely in this context:

students[  0] = {"Alex",    78.9};
Mikhail
  • 20,685
  • 7
  • 70
  • 146
  • Hi, I just tested, seems `students[ 0] = StudentStruct{"Alex", 78.9};` works but `students[ 0] = {"Alex", 78.9};` doesnt. Btw, do you know if the 1st approach has any name? Some fancy ones just like "brace initialization"? – D.J. Elkind Feb 06 '23 at 14:33
  • Both options should work. Maybe you have an outdated compiler? https://gcc.godbolt.org/z/jYdP1KEPK – Mikhail Feb 06 '23 at 15:13
  • 1st option - not sure about the _exact_ name, this stuff is complicated in c++, but it does aggregate initialization here – Mikhail Feb 06 '23 at 15:14
  • I added my compiler info to the question (g++ 10.2.1), it doesn't work online either: https://gcc.godbolt.org/z/nnPzq3ce4 – D.J. Elkind Feb 07 '23 at 01:36
  • @D.J.Elkind It is a bug. Version 11 and up all compile the code. – NathanOliver Feb 07 '23 at 02:29
  • oh really? I use `g++ 10.2.1` because it ships with Debian's stable version. So I did not pay much attention in this regard prior to this question. – D.J. Elkind Feb 07 '23 at 02:36
  • With C++ it is really important to keep your compilers up to date. A lot of bugs get fixed and a lot of missing features are added every release. Usually once you see a problem like this, it is a good reason to update the compiler. – Mikhail Feb 07 '23 at 12:08