struct member initialization

Discussion in 'C Programming' started by ssylee, Aug 22, 2008.

  1. ssylee

    ssylee Guest

    I'm not sure if I can initialize members of a struct the lazy way. For
    example, let's say I have a struct defined as below:

    typedef struct _ABC
    {
    BOOL A;
    BOOL B;
    BOOL C;
    } ABC;

    If I want to initialize all members of the struct as false, is there a
    way of changing the state of the members all at once like what I have
    attempted below:

    ABC hi = FALSE; // or something along similar lines as this as this is
    invalid

    or would the only way of modifying the members be modifying each
    member's state individually?
    ssylee, Aug 22, 2008
    #1
    1. Advertising

  2. On 22 Ago, 20:44, ssylee <> wrote:
    > I'm not sure if I can initialize members of a struct the lazy way. For
    > example, let's say I have a struct defined as below:
    >
    > typedef struct _ABC
    > {
    >        BOOL  A;
    >        BOOL  B;
    >        BOOL  C;
    >
    > } ABC;
    >
    > If I want to initialize all members of the struct as false, is there a
    > way of changing the state of the members all at once like what I have
    > attempted below:
    >
    > ABC hi = FALSE; // or something along similar lines as this as this is
    > invalid
    >
    > or would the only way of modifying the members be modifying each
    > member's state individually?


    int main(void)
    {
    ABC x = { FALSE, FALSE, FALSE };

    /* .... */
    }
    Sergio Perticone, Aug 22, 2008
    #2
    1. Advertising

  3. ssylee

    Guest

    ssylee wrote:
    > I'm not sure if I can initialize members of a struct the lazy way. For
    > example, let's say I have a struct defined as below:
    >
    > typedef struct _ABC
    > {
    > BOOL A;
    > BOOL B;
    > BOOL C;
    > } ABC;
    >
    > If I want to initialize all members of the struct as false, is there a
    > way of changing the state of the members all at once like what I have
    > attempted below:
    >
    > ABC hi = FALSE; // or something along similar lines as this as this is
    > invalid
    >
    > or would the only way of modifying the members be modifying each
    > member's state individually?


    There is a lazy way, but not quite as lazy as you would like. If you
    explicitly initialize any element of an array or any member of a
    struct, all of the elements and members that you do not explicitly
    initialize are zero-initialized. Zero-initialized means that you get
    the same result if as if you explicitly assigned a 0 to the object.
    For pointer types, that means that the pointer has a null pointer
    value.

    Therefore, if you write:

    ABC hi = {0};

    that statement explicitly initialize hi.A to 0, and causes hi.B and
    hi.C to be implicitly zero-initialized.

    The fact that all of the members of hi are set to 0 by the above
    statement leads to a common mistake. Some people incorrectly
    generalize it, by thinking that , for instance,

    int array[20] = {100};

    will cause all 20 elements to be set to 100. That is not the case - It
    will cause the first element to be set to 100, and the remaining 19
    elements to 0.

    A C99 feature allows you to explicitly initialize any particular
    element or member you want. The remaining members are still zero-
    initialized, as usual:

    ABC hi = {.C=1};

    The above statement explicitly sets hi.C to 1, while hi.A and hi.B are
    implicitly set to 0.
    , Aug 22, 2008
    #3
  4. ssylee

    Guest

    On Aug 22, 1:44 pm, ssylee <> wrote:
    > I'm not sure if I can initialize members of a struct the lazy way. For
    > example, let's say I have a struct defined as below:
    >
    > typedef struct _ABC
    > {
    > BOOL A;
    > BOOL B;
    > BOOL C;
    >
    > } ABC;
    >
    > If I want to initialize all members of the struct as false, is there a
    > way of changing the state of the members all at once like what I have
    > attempted below:
    >
    > ABC hi = FALSE; // or something along similar lines as this as this is
    > invalid
    >


    The initializer for a structure is usually a brace-enclosed set of
    values:

    ABC hi = {FALSE, FALSE, FALSE};

    The "lazy" way would be to provide the value of only one member:

    ABC hi = {FALSE};

    In this case, the rest of the members are automatically initialized to
    zero. Note that this will only work if your 'FALSE' identifier has the
    value 0. I'd recommend you to use the 'bool' type defined in
    <stdbool.h>, along with the macros 'true' and 'false', instead of
    defining your own. Using those macros this technique would work.

    In case you want to initialize some of the members to TRUE and the
    rest to FALSE, you can do something like this:

    ABC hi = {.A = TRUE, .C = TRUE};

    > or would the only way of modifying the members be modifying each
    > member's state individually?


    If it's at initialization, you can use the techniques described above.
    Otherwise, if you already have the structure and want to change its
    state, the rules are a bit different. One way that would be similar to
    initialization would be to use a compound literal:

    ABC hi;
    ....
    hi = (struct ABC) {FALSE}; // this is assignment, not initialization

    In this case, we didn't specify a value for all the members in the
    compound literal, so the rest of them are initialized to zero. Another
    way to do it would be to use memset():

    ABC hi;
    ....
    memset(hi, (int) FALSE, sizeof hi);

    But this technique is rather cryptic. (Note that I casted 'FALSE' to
    int to emphasize that the second argument to memset() is an int, which
    means that your 'FALSE' identifier should be able to be converted to
    int.)

    Sebastian
    , Aug 22, 2008
    #4
  5. ssylee

    ssylee Guest

    On Aug 22, 1:34 pm, wrote:
    > On Aug 22, 1:44 pm, ssylee <> wrote:
    >
    >
    >
    > > I'm not sure if I can initialize members of a struct the lazy way. For
    > > example, let's say I have a struct defined as below:

    >
    > > typedef struct _ABC
    > > {
    > >        BOOL  A;
    > >        BOOL  B;
    > >        BOOL  C;

    >
    > > } ABC;

    >
    > > If I want to initialize all members of the struct as false, is there a
    > > way of changing the state of the members all at once like what I have
    > > attempted below:

    >
    > > ABC hi = FALSE; // or something along similar lines as this as this is
    > > invalid

    >
    > The initializer for a structure is usually a brace-enclosed set of
    > values:
    >
    > ABC hi = {FALSE, FALSE, FALSE};
    >
    > The "lazy" way would be to provide the value of only one member:
    >
    > ABC hi = {FALSE};
    >
    > In this case, the rest of the members are automatically initialized to
    > zero. Note that this will only work if your 'FALSE' identifier has the
    > value 0. I'd recommend you to use the 'bool' type defined in
    > <stdbool.h>, along with the macros 'true' and 'false', instead of
    > defining your own. Using those macros this technique would work.
    >
    > In case you want to initialize some of the members to TRUE and the
    > rest to FALSE, you can do something like this:
    >
    > ABC hi = {.A = TRUE, .C = TRUE};
    >
    > > or would the only way of modifying the members be modifying each
    > > member's state individually?

    >
    > If it's at initialization, you can use the techniques described above.
    > Otherwise, if you already have the structure and want to change its
    > state, the rules are a bit different. One way that would be similar to
    > initialization would be to use a compound literal:
    >
    > ABC hi;
    > ...
    > hi = (struct ABC) {FALSE}; // this is assignment, not initialization
    >
    > In this case, we didn't specify a value for all the members in the
    > compound literal, so the rest of them are initialized to zero. Another
    > way to do it would be to use memset():
    >
    > ABC hi;
    > ...
    > memset(hi, (int) FALSE, sizeof hi);
    >
    > But this technique is rather cryptic. (Note that I casted 'FALSE' to
    > int to emphasize that the second argument to memset() is an int, which
    > means that your 'FALSE' identifier should be able to be converted to
    > int.)
    >
    > Sebastian


    Thank you for all your suggestions. I guess I was looking for ABC hi =
    {FALSE}; but couldn't seem to recall at the moment I was asking the
    question.
    ssylee, Aug 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,335
    Chris Fogelklou
    Apr 20, 2004
  2. Replies:
    3
    Views:
    823
    Peter_Julian
    Oct 10, 2005
  3. Replies:
    2
    Views:
    710
    David Harmon
    Sep 20, 2006
  4. Peter Liu
    Replies:
    4
    Views:
    335
    Peter Liu
    Jan 19, 2007
  5. aaragon
    Replies:
    2
    Views:
    599
    James Kanze
    Nov 2, 2008
Loading...

Share This Page