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


Groups > comp.lang.c > #138726

Re: assigning constant values to complex structure

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
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 <lno99jefb2.fsf@kst-u.example.com> (permalink)
References <pv9jcr$qg0$1@gioia.aioe.org>
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

Show key headers only | View raw


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.)

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  <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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