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


Groups > comp.lang.c > #138724 > unrolled thread

assigning constant values to complex structure

Started bychano <chano@nospam.no>
First post2018-12-18 01:43 +0000
Last post2018-12-19 14:59 -0800
Articles 14 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  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

#138724 — assigning constant values to complex structure

Fromchano <chano@nospam.no>
Date2018-12-18 01:43 +0000
Subjectassigning constant values to complex structure
Message-ID<pv9jcr$qg0$1@gioia.aioe.org>
Hello everyone

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 };

[toc] | [next] | [standalone]


#138725

FromBart <bc@freeuk.com>
Date2018-12-18 02:05 +0000
Message-ID<TBYRD.63639$krL1.1252@fx15.am4>
In reply to#138724
On 18/12/2018 01:43, chano wrote:
> 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 }

This seemed to work with gcc (note list names need to be different):

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;


int main(void){
   a = ((struct letteroclass_bin){ L'a', 1 }) ;
}

This used a compound literal. Otherwise:

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 } ;

But the elements must be constants.


-- 
bart

[toc] | [prev] | [next] | [standalone]


#138727

FromKeith Thompson <kst-u@mib.org>
Date2018-12-17 18:56 -0800
Message-ID<lnk1k7ee5c.fsf@kst-u.example.com>
In reply to#138725
Bart <bc@freeuk.com> writes:
[...]

> 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 } ;
>
> But the elements must be constants.

In C99 or later, the elements need not be constant expressions unless
the object is static (defined with the "static" keyword or at file
scope).

-- 
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"

[toc] | [prev] | [next] | [standalone]


#138726

FromKeith Thompson <kst-u@mib.org>
Date2018-12-17 18:31 -0800
Message-ID<lno99jefb2.fsf@kst-u.example.com>
In reply to#138724
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"

[toc] | [prev] | [next] | [standalone]


#138736

Fromchano <chano@nospam.no>
Date2018-12-18 18:11 +0000
Message-ID<pvbd93$p56$1@gioia.aioe.org>
In reply to#138726
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]
 }
 ^

 

[toc] | [prev] | [next] | [standalone]


#138737

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-18 10:58 -0800
Message-ID<e73c4d08-d45b-4e28-aa99-d9307226e3d8@googlegroups.com>
In reply to#138736
On Tuesday, December 18, 2018 at 1:11:58 PM UTC-5, chano wrote:
...
> 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 };

You're trying to initialize unsigned char objects with wide character
constants. If you want to use char objects, you should initialize with
ordinary character constants such as 'a'. If you want to use wide
character constants such as L'a', you should declare your arrays as
"wchar_t", not "unsigned char". Only you can know for sure which is the
better approach. However, your use of wint_t suggests that changing to
wchar_t might be the correct solution, so I'll assume that in the rest
of my response.

You're initializing each element of the arrays separately, which is perfectly legal, but there's a short cut for arrays of character type, which would simplify the above code:

={{L"ab", L"cd", L"ef"}, 1}.

In C90, the only member of a union you could initialize was the first
one, in this case a.letteroclass.l, and you've provided too many
initializers for that object. Your only solution would have been to make
jamolist the first member of the union:

struct letteroclass_bin
{
    union
    {
        struct
        {
            wchar_t clist[20];
            wchar_t jlist[22];
            wchar_t jolist[29];
        } jamolist;
        wint_t l;       // letter;
    } letteroclass;
    SMALLINT current_data_type;
} a =
{{L"ab", L"cd", L"ef"}, 1 };

This is also the simplest solution. In C99 and later, you could
initialize a different member of a union, but only by use of a
designated initializer:

struct letteroclass_bin
{
    union
    {
        wint_t l;       // letter;
        struct
        {
            wchar_t clist[20];
            wchar_t jlist[22];
            wchar_t jolist[29];
        } jamolist;
    } letteroclass;
    SMALLINT current_data_type;
} a =
{{.jamolist={L"ab", L"cd", L"ef"}}, 1 }; 

I can't test right now, so I'm not sure I got some of the fiddly details
right, such as the number of levels of curly brackets. I'll let you
figure those out.

[toc] | [prev] | [next] | [standalone]


#138738

FromKeith Thompson <kst-u@mib.org>
Date2018-12-18 12:05 -0800
Message-ID<lnd0pyeh2j.fsf@kst-u.example.com>
In reply to#138736
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"

[toc] | [prev] | [next] | [standalone]


#138746

Fromchano <chano@nospam.no>
Date2018-12-19 19:44 +0000
Message-ID<pve73j$cms$1@gioia.aioe.org>
In reply to#138726
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

[toc] | [prev] | [next] | [standalone]


#138749

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-19 11:50 -0800
Message-ID<6a7f6d23-5a15-4f27-8c59-90ddbb802a1d@googlegroups.com>
In reply to#138746
On Wednesday, December 19, 2018 at 2:45:02 PM UTC-5, chano wrote:
> On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:
...
> > 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?

A compound literal.

[toc] | [prev] | [next] | [standalone]


#138750

Fromchano <chano@nospam.no>
Date2018-12-19 20:27 +0000
Message-ID<pve9jl$otk$1@gioia.aioe.org>
In reply to#138749
On Wed, 19 Dec 2018 11:50:23 -0800, James Kuyper wrote:

> On Wednesday, December 19, 2018 at 2:45:02 PM UTC-5, chano wrote:
>> On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:
> ...
>> > 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?
> 
> A compound literal.

And what should I read to know all these? like complex struct, compound 
literal, or only first of union can be assigned in c89 ... etc?

I'm reading "c programming language" by kernighan and it's not there.
other books are just another beginner books or references.

should I read standard paper to know this? ( which probably it's really 
hard to figure out what the contents of paper mean)

[toc] | [prev] | [next] | [standalone]


#138751

FromWilliam Ahern <william@25thandClement.com>
Date2018-12-19 13:20 -0800
Message-ID<89pqef-8rv.ln1@wilbur.25thandClement.com>
In reply to#138750
chano <chano@nospam.no> wrote:
> On Wed, 19 Dec 2018 11:50:23 -0800, James Kuyper wrote:
> 
>> On Wednesday, December 19, 2018 at 2:45:02 PM UTC-5, chano wrote:
>>> On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:
>> ...
>>> > 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?
>> 
>> A compound literal.
> 
> And what should I read to know all these? like complex struct, compound 
> literal, or only first of union can be assigned in c89 ... etc?
> 
> I'm reading "c programming language" by kernighan and it's not there.
> other books are just another beginner books or references.
> 
> should I read standard paper to know this? ( which probably it's really 
> hard to figure out what the contents of paper mean)

N1570 was the final draft of C11 before publication:

  http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

As standards go it's very accessible (i.e. useful as a learning tool), and
IMO more accessible than most language documentation, period.

The C Programming Language (aka the K&R book, what you have now) and the
standard itself is all you really need. In terms of reference and
explanation, most everything else is a distraction. Forums like this are
useful for helping one to formulate the right questions (e.g. "what's a
compound literal?"), not for answering language questions, per se.

It's preferable that when reading the standard your PDF viewer supports
displaying the Table of Contents as a sidebar. It makes it much easier to
navigate, especially when you're not quite sure where to look for answers.

[toc] | [prev] | [next] | [standalone]


#138752

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-19 14:08 -0800
Message-ID<ec96ee11-6a53-45f9-9a92-04aeed04d5fe@googlegroups.com>
In reply to#138750
On Wednesday, December 19, 2018 at 3:27:43 PM UTC-5, chano wrote:
> On Wed, 19 Dec 2018 11:50:23 -0800, James Kuyper wrote:
> 
> > On Wednesday, December 19, 2018 at 2:45:02 PM UTC-5, chano wrote:
> >> On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:
> > ...
> >> > 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?
> > 
> > A compound literal.
> 
> And what should I read to know all these? like complex struct, compound 
> literal, or only first of union can be assigned in c89 ... etc?
> 
> I'm reading "c programming language" by kernighan and it's not there.
> other books are just another beginner books or references.
> 
> should I read standard paper to know this? ( which probably it's really 
> hard to figure out what the contents of paper mean)

The standard is a reference work, and you should eventually familiarize
yourself with it. However, your starting place should be a C textbook. I
learned C in 1979 from the "C Programming Language", and I found it very
easy to understand. However, at the time I read it, I was already
familiar with four other programming languages. I've heard that it is
not as good of a textbook for people who are learning C as their first
programming language.

In any event, the book I read is now 4 decades out of date; the second
edition was updated to match the first version of the C standard, C89,
and is therefore now "only" 3 decades out of date. Furthermore, it was
based upon a draft version of  C89, and therefore is slightly
inconsistent with the final version of C89. I still recommend reading
it, but that should only be the starting point of your C education.

I'd strongly recommend following it with an up-to-date C textbook - but
it's been a very long time since I needed a textbook rather than a
reference work, so I can't recommend any particular one.

[toc] | [prev] | [next] | [standalone]


#138761

FromDavid Brown <david.brown@hesbynett.no>
Date2018-12-20 08:15 +0100
Message-ID<pvffht$hue$1@dont-email.me>
In reply to#138750
On 19/12/2018 21:27, chano wrote:
> On Wed, 19 Dec 2018 11:50:23 -0800, James Kuyper wrote:
> 
>> On Wednesday, December 19, 2018 at 2:45:02 PM UTC-5, chano wrote:
>>> On Mon, 17 Dec 2018 18:31:29 -0800, Keith Thompson wrote:
>> ...
>>>> 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?
>>
>> A compound literal.
> 
> And what should I read to know all these? like complex struct, compound
> literal, or only first of union can be assigned in c89 ... etc?
> 
> I'm reading "c programming language" by kernighan and it's not there.
> other books are just another beginner books or references.
> 
> should I read standard paper to know this? ( which probably it's really
> hard to figure out what the contents of paper mean)
> 

If you want an on-line reference, then I recommend en.cppreference.com. 
It covers C++ and C, with a strong emphasis on being accurate to the 
standards while being a little more approachable and clearer.

<https://en.cppreference.com/w/c/language/compound_literal>



[toc] | [prev] | [next] | [standalone]


#138753

FromSiri Cruise <chine.bleu@yahoo.com>
Date2018-12-19 14:59 -0800
Message-ID<chine.bleu-7C7764.14593519122018@reader.eternal-september.org>
In reply to#138724
In article <pv9jcr$qg0$1@gioia.aioe.org>, chano <chano@nospam.no> wrote:

> I have struct definition like this one
> 
> struct letteroclass_bin

> it's pretty complex structure for myself
> what's the way to assign constant value to such a structure?

A different way to approach this is

    #define variable (*variable_())
    Type *variable_ (void) {
        static Type v; static int first = 1;
        if (first) {
            v = definition;
            moredefinition(v);
            evenmoredefinition(v);
            first = 0;
        }
        return &v;
    }

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.    @
'I desire mercy, not sacrifice.'                                    /|\
The first law of discordiamism: The more energy   This post         / \
to make order is nore energy made into entropy.   insults Islam.  Mohammed

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web