Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #138738
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: assigning constant values to complex structure |
| Date | 2018-12-18 12:05 -0800 |
| Organization | None to speak of |
| Message-ID | <lnd0pyeh2j.fsf@kst-u.example.com> (permalink) |
| References | <pv9jcr$qg0$1@gioia.aioe.org> <lno99jefb2.fsf@kst-u.example.com> <pvbd93$p56$1@gioia.aioe.org> |
chano <chano@nospam.no> writes:
[...]
> now how do I assign array of constant values to a with this complexity?
>
> I tried
>
> #include <wchar.h>
> #define SMALLINT char
If you want to define an alias for a type, use as typedef, not a macro.
(And all-caps identifiers are conventionally used for macro names.)
typedef char smallint;
> 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()
"int main()" is ok, but "int main(void)" is preferred.
> {
> //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]
> }
> ^
Unless you use designators (.membername = value), you can only
initialize the first element of a union. The compiler assumes
you're trying to initialize the l member of the union, not the
jamolist member. I think most of the errors follow from that.
#include <wchar.h>
typedef char smallint;
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 = {
.letteroclass = {
.jamolist = {
.clist = "ab",
.jlist = "cd",
.jolist = "ef"
},
},
.current_data_type = 1
};
int main(void)
{
// ...
}
And to use designators, you have to be using C99 or later. You appear
to be using gcc, or perhaps clang, so use one of -std=c99, -std=gnu99,
-std=c11, -std=gnu11.
(C99 added a rule making a return statement at the end of main()
optional. I can tell you're using C89/C90 because the compiler warned
about the missing return statement.)
For a complicated structure like this, designators will make the code
much easer to read (and to write).
If clist, jlist, and jolist are meant to hold textual data, they
should probably be char[], not unsigned char[]. It's hard to tell
which you should use, but you seem to be storing nul-terminated
strings in them. Either way, the elements are not wide characters,
so don't use L'a' etc.
Here's my version of your code:
#include <wchar.h>
typedef char smallint;
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 = {
.letteroclass = {
.jamolist = {
.clist = "ab",
.jlist = "cd",
.jolist = "ef"
},
},
.current_data_type = 1
};
int main(void)
{
// ...
}
You can replace "ab" with {'a', 'b', '\0'} if you prefer.
--
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
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