Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #138746

Re: assigning constant values to complex structure

From chano <chano@nospam.no>
Newsgroups comp.lang.c
Subject Re: assigning constant values to complex structure
Date 2018-12-19 19:44 +0000
Organization Aioe.org NNTP Server
Message-ID <pve73j$cms$1@gioia.aioe.org> (permalink)
References <pv9jcr$qg0$1@gioia.aioe.org> <lno99jefb2.fsf@kst-u.example.com>

Show all headers | View raw


On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:

> chano <chano@nospam.no> 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.)
if this is not cast expression, what is it called?
just for curiosity

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

assigning constant values to complex structure chano <chano@nospam.no> - 2018-12-18 01:43 +0000
  Re: assigning constant values to complex structure Bart <bc@freeuk.com> - 2018-12-18 02:05 +0000
    Re: assigning constant values to complex structure Keith Thompson <kst-u@mib.org> - 2018-12-17 18:56 -0800
  Re: assigning constant values to complex structure Keith Thompson <kst-u@mib.org> - 2018-12-17 18:31 -0800
    Re: assigning constant values to complex structure chano <chano@nospam.no> - 2018-12-18 18:11 +0000
      Re: assigning constant values to complex structure James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-18 10:58 -0800
      Re: assigning constant values to complex structure Keith Thompson <kst-u@mib.org> - 2018-12-18 12:05 -0800
    Re: assigning constant values to complex structure chano <chano@nospam.no> - 2018-12-19 19:44 +0000
      Re: assigning constant values to complex structure James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-19 11:50 -0800
        Re: assigning constant values to complex structure chano <chano@nospam.no> - 2018-12-19 20:27 +0000
          Re: assigning constant values to complex structure William Ahern <william@25thandClement.com> - 2018-12-19 13:20 -0800
          Re: assigning constant values to complex structure James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-19 14:08 -0800
          Re: assigning constant values to complex structure David Brown <david.brown@hesbynett.no> - 2018-12-20 08:15 +0100
  Re: assigning constant values to complex structure Siri Cruise <chine.bleu@yahoo.com> - 2018-12-19 14:59 -0800

csiph-web