Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: assigning constant values to complex structure Date: Mon, 17 Dec 2018 18:31:29 -0800 Organization: None to speak of Lines: 80 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="293fcf4c88fbd1418164e5e9e2c7a0cd"; logging-data="11079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+k9hsMk930FHHzkGXxOMm0" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:R27RBoFsAxtiz0x/sGyVCkdMgnM= sha1:GS8YNs4ZNrs7Dd0ypX5TvYkFI/U= Xref: csiph.com comp.lang.c:138726 chano writes: > I have struct definition like this one > > struct letteroclass_bin > { > union > { > wint_t l; // letter; > struct > { > unsigned char list[20]; > unsigned char list[22]; > unsigned char list[29]; > } jlist; > } letteroclass; > unsigned char current_data_type; > } a; > > it's pretty complex structure for myself > what's the way to assign constant value to such a structure? > > I tried > > a = { L'a', 1 } > > and didn't work and I can't guess how to correctly assign constant values > from error message. > > gcc says > jstruct.c:21:9: error: expected expression before '{' token > a = { L'a' , 1 }; Please copy-and-paste your exact type definition. Unless there's something serously wrong with your compiler, it would have rejected that definition because your nested structure has three members all named "list". I'll assume the arrays have distinct names. The simplest way is to initialize it rather than assigning to it -- but you can do that only once for each object: struct letteroclass_bin { union { wint_t l; // letter; struct { unsigned char list1[20]; unsigned char list2[22]; unsigned char list3[29]; } jlist; } letteroclass; unsigned char current_data_type; } a = { L'a', 1 }; A comma-separated list of expression enclosed in curly braces can be used as an initializer, but it's not an expression, so you can't use this syntax to assign a value to a later on. If your compiler supports C99 or later, you can use a compound literal: a = (struct letteroclass_bin){ L'a', 1 }; (Note that this is not a cast expression.) Given the complexity of your structure type, it's probably a good idea to use designators to specify which member(s) you're initializing: a = (struct letteroclass_bin){ .l = L'a', .current_data_type = 1 }; As in an initalizer, any members not specified are set to zero. (Incidentally, the name "l" could cause problems. Depending on the font, it can look too much like an upper case "I" or a digit "1".) -- Keith Thompson (The_Other_Keith) kst@mib.org Will write code for food. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"