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


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

C struct member default values in struct

Started by"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
First post2015-10-21 20:31 -0700
Last post2015-11-04 11:07 -0800
Articles 7 — 3 participants

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


Contents

  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

#73527 — C struct member default values in struct

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-10-21 20:31 -0700
SubjectC struct member default values in struct
Message-ID<fbf62a6a-e135-404c-8369-eee4e9a5738e@googlegroups.com>
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?

Best regards,
Rick C. Hodgin

[toc] | [next] | [standalone]


#73528

Fromsupercat@casperkitty.com
Date2015-10-21 21:21 -0700
Message-ID<5f6324dc-eaf5-4bf2-8772-9a034b3ad080@googlegroups.com>
In reply to#73527
On Wednesday, October 21, 2015 at 10:31:35 PM UTC-5, Rick C. Hodgin wrote:
> Is there a version of C or C++ extension which takes this...

It's possible to do a lot with the preprocessor, though in some cases you
may have to define a macro called something like "COMMA" to ensure that you
can embed commas in within a macro argument.  I've done used macros to let
met me define default values for a structure at the same time as I defined
the structure itself (that was especially useful before named-field initial-
izers were added to the syntax).  I'm not sure how best to set up the macros
for what you have in mind, but it would seem fairly straightforward.

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


#73541

FromFrank Tetzel <s1445051@mail.zih.tu-dresden.de>
Date2015-10-22 13:37 +0200
Message-ID<20151022133715.6f7c74e8@archthink>
In reply to#73527
> 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

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


#73552

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-10-22 06:58 -0700
Message-ID<30e5e12b-64a5-430e-8fb7-df00f01f0372@googlegroups.com>
In reply to#73541
On Thursday, October 22, 2015 at 7:38:24 AM UTC-4, Frank Tetzel wrote:
> > 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}
>     };

Is the "=" optional in C?  Or just C++11?

-----
The rest of my response is off-topic for the C group, but if you're
interested in seeing my CAlive implementation, go here:

    https://groups.google.com/d/msg/rapid_development_compiler/FjprgbxfAiU/r_-lVoi0CgAJ

Best regards,
Rick C. Hodgin

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


#73560

FromFrank Tetzel <s1445051@mail.zih.tu-dresden.de>
Date2015-10-22 17:27 +0200
Message-ID<20151022172730.2da71c4a@archthink>
In reply to#73552
> > 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}
> >     };  
> 
> Is the "=" optional in C?  Or just C++11?

Not in C as far as i know. It's new in C++11, see:

http://en.cppreference.com/w/cpp/language/list_initialization
http://en.cppreference.com/w/cpp/language/aggregate_initialization

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


#74863

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-11-04 11:05 -0800
Message-ID<01a07738-2595-4790-a655-d183bc4f6bea@googlegroups.com>
In reply to#73527
Because member default values are introduced, CAlive will also introduce
non-constructor instantiation of class objects when used as follows:

    char invalid_data[] = "Invalid";

    class CAbc
    {
    public:
        CAbc(char* p) : m_p(p) { }
        ~CAbc() { }

    private:
        char* p = invalid_data;
    }


    void function(void)
    {
        // No constructor will be called, m_p initialized to "Invalid"
        CAbc* abc = lnew CAbc;

        // Constructor will be called, m_p initialized to "Rick"
        CAbc* abc = lnew CAbc("Rick");
    }

Best regards,
Rick C. Hodgin

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


#74864

From"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Date2015-11-04 11:07 -0800
Message-ID<2373d976-47b8-41d1-aa6c-4cb7d6542e01@googlegroups.com>
In reply to#74863
On Wednesday, November 4, 2015 at 2:06:16 PM UTC-5, Rick C. Hodgin wrote:
>     private:
>         char* p = invalid_data;

Should be m_p, not p.

Best regards,
Rick C. Hodgin

[toc] | [prev] | [standalone]


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


csiph-web