Have you tried to use an intermediate variable of type RegType (not an array of Regtype) and first do a (non blocking) assignment to that temp variable and then use the index into the temp variable, something like:. Tempstructholder = REGISTERBANKINFON; iSCONTROLREGISTERSd1N.
Active1 year, 7 months ago
I'm trying to create an array of structs. Is the code below valid? I keep getting an
ddaexpected primary-expression before '{'
token error.5,52422 gold badges2020 silver badges3333 bronze badges
user637965
4 Answers
Tuomas PelkonenTuomas Pelkonen7,27722 gold badges2626 silver badges3030 bronze badges
You can't use an initialization-list for a
struct
after it's been initialized. You've already default-initialized the two Customer
structs when you declared the array customerRecords
. Therefore you're going to have either use member-access syntax to set the value of the non-static data members, initialize the structs using a list of initialization lists when you declare the array itself, or you can create a constructor for your struct and use the default operator=
member function to initialize the array members.So either of the following could work:
Or if you defined a constructor for your struct like:
You could then do:
Or you could do the sequence of initialization lists that Tuomas used in his answer. The reason his initialization-list syntax works is because you're actually initializing the
Customer
structs at the time of the declaration of the array, rather than allowing the structs to be default-initialized which takes place whenever you declare an aggregate>66 gold badges5252 silver badges7171 bronze badgesSome compilers support compound literals as an extention, allowing this construct:
But it's rather unportable.
DaveDave9,70311 gold badge2323 silver badges5050 bronze badges
It works perfectly. I have gcc compiler C++11 ready.Try this and you'll see:
MatoMato