Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #138736
| From | chano <chano@nospam.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: assigning constant values to complex structure |
| Date | 2018-12-18 18:11 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <pvbd93$p56$1@gioia.aioe.org> (permalink) |
| References | <pv9jcr$qg0$1@gioia.aioe.org> <lno99jefb2.fsf@kst-u.example.com> |
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.)
>
> 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".)
#include <wchar.h>
#define SMALLINT char
struct letteroclass_bin
{
union
{
wint_t l; // letter;
struct
{
unsigned char clist[20];
unsigned char jlist[22];
unsigned char jolist[29];
} jamolist;
} letteroclass;
SMALLINT current_data_type;
} a = { { L'a' }, 1 };
int main()
{
a = (struct letteroclass_bin) { {.l =L'a' } , .current_data_type=1, };
wprintf(L"%lc %d\n" , a.letteroclass.l, a.current_data_type);
}
after adding an additional pair of braces, I was able to compile
successfully
Thank you Bart and Keith
now how do I assign array of constant values to a with this complexity?
I tried
#include <wchar.h>
#define SMALLINT char
struct letteroclass_bin
{
union
{
wint_t l; // letter;
struct
{
unsigned char clist[20];
unsigned char jlist[22];
unsigned char jolist[29];
} jamolist;
} letteroclass;
SMALLINT current_data_type;
} a = {{{L'a', L'b', L'\0'},{L'c', L'd', L'\0'},{L'e', L'f', L'\0'}}, 1 };
int main()
{
//a = (struct letteroclass_bin) { {.l =
L'a' } , .current_data_type=1, };
//wprintf(L"%lc %d\n" , a.letteroclass.l, a.current_data_type);
}
I got below warnings
jstruct.c:17:1: warning: braces around scalar initializer [enabled by
default]
} a = {{{L'a', L'b', L'\0'},{L'c', L'd', L'\0'},{L'e', L'f', L'\0'}}, 1 };
^
jstruct.c:17:1: warning: (near initialization for 'a.letteroclass.l')
[enabled by default]
jstruct.c:17:1: warning: excess elements in scalar initializer [enabled by
default]
jstruct.c:17:1: warning: (near initialization for 'a.letteroclass.l')
[enabled by default]
jstruct.c:17:1: warning: excess elements in scalar initializer [enabled by
default]
jstruct.c:17:1: warning: (near initialization for 'a.letteroclass.l')
[enabled by default]
jstruct.c:17:1: error: extra brace group at end of initializer
jstruct.c:17:1: error: (near initialization for 'a.letteroclass')
jstruct.c:17:1: warning: excess elements in union initializer [enabled by
default]
jstruct.c:17:1: warning: (near initialization for 'a.letteroclass')
[enabled by default]
jstruct.c:17:1: error: extra brace group at end of initializer
jstruct.c:17:1: error: (near initialization for 'a.letteroclass')
jstruct.c:17:1: warning: excess elements in union initializer [enabled by
default]
jstruct.c:17:1: warning: (near initialization for 'a.letteroclass')
[enabled by default]
jstruct.c: In function 'main':
jstruct.c:24:1: warning: control reaches end of non-void function [-
Wreturn-type]
}
^
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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