struct member initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ssylee

    struct member initialization

    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?
  • Sergio Perticone

    #2
    Re: struct member initialization

    On 22 Ago, 20:44, ssylee <staniga...@gma il.comwrote:
    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 };

    /* .... */
    }

    Comment

    • jameskuyper@verizon.net

      #3
      Re: struct member initialization

      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.

      Comment

      • pete

        #4
        Re: struct member initialization

        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?

        /* BEGIN new.c */

        typedef struct ABC {
        int A;
        int B;
        int C;
        } ABC;

        int main(void)
        {
        ABC lo;
        ABC hi = {0};

        lo = hi;
        return lo.C;
        }

        /* END new.c */


        --
        pete

        Comment

        • s0suk3@gmail.com

          #5
          Re: struct member initialization

          On Aug 22, 1:44 pm, ssylee <stanigator@gma il.comwrote:
          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

          Comment

          • ssylee

            #6
            Re: struct member initialization

            On Aug 22, 1:34 pm, s0s...@gmail.co m wrote:
            On Aug 22, 1:44 pm, ssylee <staniga...@gma il.comwrote:
            >
            >
            >
            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.

            Comment

            Working...