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


Groups > comp.lang.c > #73541

Re: C struct member default values in struct

From Frank Tetzel <s1445051@mail.zih.tu-dresden.de>
Newsgroups comp.lang.c
Subject Re: C struct member default values in struct
Date 2015-10-22 13:37 +0200
Message-ID <20151022133715.6f7c74e8@archthink> (permalink)
References <fbf62a6a-e135-404c-8369-eee4e9a5738e@googlegroups.com>

Show all headers | View raw


> Is there a version of C or C++ extension which takes this:
> 
>     struct SAbc
>     {
>         int a;
>         int b;
>         int c;
>     };
> 
>     struct SAbc myAbcVar[] =
>     {
>         { 1, 2, 3 },
>         { 4, 5, 6 },
>         { 7, 8, 9 }
>     };
> 
> And lets me change my structure to this:
> 
>     struct SAbc
>     {
>         int a;
>         int b;
>         int c;
>         int d;
>     };
> 
> Without having to do this manually:
> 
>     struct SAbc myAbcVar[] =
>     {
>         { 1, 2, 3, -1 },
>         { 4, 5, 6, -1 },
>         { 7, 8, 9, -1 }
>     };
> 
> By allowing me instead to do something like this:
> 
>     struct SAbc
>     {
>         int a;
>         int b;
>         int c;
>         int d     = -1;  // Default all unspecified values to -1
>     };
> 
> ?
> 
> Does C++ or another C-like language have the ability to either
> initialize all members in the struct definition, or to initialize
> those members which are added on to the end later, as with C++'s
> ability to initialize unspecified parameter values?

C++11 with initializer lists:

    struct SAbc
    {
        int a;
        int b;
        int c;
        int d     = -1;

        SAbc(int a, int b, int c) : a(a), b(b), c(c) {}
        SAbc(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
    };

    SAbc myAbcVar[] { // = is optional
        {0, 1, 2},
        {0, 1, 2, 3}
    };

Regards,
Frank

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


Thread

C struct member default values in struct "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-21 20:31 -0700
  Re: C struct member default values in struct supercat@casperkitty.com - 2015-10-21 21:21 -0700
  Re: C struct member default values in struct Frank Tetzel <s1445051@mail.zih.tu-dresden.de> - 2015-10-22 13:37 +0200
    Re: C struct member default values in struct "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-10-22 06:58 -0700
      Re: C struct member default values in struct Frank Tetzel <s1445051@mail.zih.tu-dresden.de> - 2015-10-22 17:27 +0200
  Re: C struct member default values in struct "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-04 11:05 -0800
    Re: C struct member default values in struct "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-04 11:07 -0800

csiph-web