Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Can a union in standard C "skip" members?

Can a union in standard C "skip" members?

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialquestion
10 Posts 4 Posters 62 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    The following is an example of a simple struct union:

    typedef union _UINT32_PART_ {
    uint32_t dwWord;

    struct {
    uint16_t dwMSB;
    uint16_t dwLSB;
    } hw;

    struct {
    uint8_t byMSB;
    uint8_t byMSBL;
    uint8_t byLSBH;
    uint8_t byLSB;
    } b;
    } DWORD_PART;

    Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:

    typedef union _UINT32_PART_ {
    uint32_t dwWord;

    struct {
    // 2 bytes empty gap
    uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
    } hw;

    struct {
    uint8_t byMSB;
    uint8_t byMSBL;
    // 1 byte empty gap
    uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
    } b;
    } DWORD_PART;

    L 0 2 Replies Last reply
    0
    • A arnold_w

      The following is an example of a simple struct union:

      typedef union _UINT32_PART_ {
      uint32_t dwWord;

      struct {
      uint16_t dwMSB;
      uint16_t dwLSB;
      } hw;

      struct {
      uint8_t byMSB;
      uint8_t byMSBL;
      uint8_t byLSBH;
      uint8_t byLSB;
      } b;
      } DWORD_PART;

      Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:

      typedef union _UINT32_PART_ {
      uint32_t dwWord;

      struct {
      // 2 bytes empty gap
      uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
      } hw;

      struct {
      uint8_t byMSB;
      uint8_t byMSBL;
      // 1 byte empty gap
      uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
      } b;
      } DWORD_PART;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No, and what would be the point?

      A 1 Reply Last reply
      0
      • L Lost User

        No, and what would be the point?

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        To divide a struct into a high-level application level part and a low-level driver part.

        L 1 Reply Last reply
        0
        • A arnold_w

          To divide a struct into a high-level application level part and a low-level driver part.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, but I do not understand what you hope to achieve. A struct is just a description of a block of memory that contains a fixed set of bytes, words etc. If it is 20 bytes long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense.

          A X 2 Replies Last reply
          0
          • L Lost User

            Sorry, but I do not understand what you hope to achieve. A struct is just a description of a block of memory that contains a fixed set of bytes, words etc. If it is 20 bytes long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense.

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            Yes, but just like you can choose to only initialize parts of a struct:

            struct demo_s {
            int first;
            int second;
            int third;
            };
            struct demo_s demo2 = { .second = 2, .third = 3 };

            I was hoping that some union names (hw or b, in the example in my first post) could omit having a reference.

            L 1 Reply Last reply
            0
            • A arnold_w

              Yes, but just like you can choose to only initialize parts of a struct:

              struct demo_s {
              int first;
              int second;
              int third;
              };
              struct demo_s demo2 = { .second = 2, .third = 3 };

              I was hoping that some union names (hw or b, in the example in my first post) could omit having a reference.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Whether you initialise all or part of the struct before using it does not matter, except for your code. But you must still define it exactly and completely.

              A 1 Reply Last reply
              0
              • L Lost User

                Whether you initialise all or part of the struct before using it does not matter, except for your code. But you must still define it exactly and completely.

                A Offline
                A Offline
                arnold_w
                wrote on last edited by
                #7

                Yes, but only struct members relevant to the application level should be initialized at the application level. The low-level driver struct members should be initialized at the low-level driver level.

                L 1 Reply Last reply
                0
                • A arnold_w

                  Yes, but only struct members relevant to the application level should be initialized at the application level. The low-level driver struct members should be initialized at the low-level driver level.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  That is irrelevant; this discussion is about the struct definition. What you do with the content is dependent on your code, but the definition (as I keep repeating) must include every element of the block of memory.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Sorry, but I do not understand what you hope to achieve. A struct is just a description of a block of memory that contains a fixed set of bytes, words etc. If it is 20 bytes long then you must describe each part exactly so the compiler can calculate the offsets correctly. Leaving blank space and expecting the compiler to guess what is missing makes no sense.

                    X Offline
                    X Offline
                    xiaofeifei66
                    wrote on last edited by
                    #9

                    successful freelancers swear by exercises — washing your face, putting on actual garments and setting telephone limitations — and people recommendations growth to consuming. nutritional therapist and longtime paintings-from-homer wilma macdonald informed huffpost that snacking proper here and there is the biggest mistake she sees new a ways flung workers make. she recommends putting in your day like several normal operating day, with a lunch damage, a few smaller breaks and a focus at the maximum essential meal: breakfast. https://www.vipblogweb.com

                    1 Reply Last reply
                    0
                    • A arnold_w

                      The following is an example of a simple struct union:

                      typedef union _UINT32_PART_ {
                      uint32_t dwWord;

                      struct {
                      uint16_t dwMSB;
                      uint16_t dwLSB;
                      } hw;

                      struct {
                      uint8_t byMSB;
                      uint8_t byMSBL;
                      uint8_t byLSBH;
                      uint8_t byLSB;
                      } b;
                      } DWORD_PART;

                      Is is possible to somehow omit, for example dwMSB and byLSBH, without making the subsequent members point to the wrong things? So, basically this is what I want:

                      typedef union _UINT32_PART_ {
                      uint32_t dwWord;

                      struct {
                      // 2 bytes empty gap
                      uint16_t dwLSB; // This must still point to the 2nd word, not the 1st!
                      } hw;

                      struct {
                      uint8_t byMSB;
                      uint8_t byMSBL;
                      // 1 byte empty gap
                      uint8_t byLSB; // This must still point to the 4th byte, not the 3rd!
                      } b;
                      } DWORD_PART;

                      0 Offline
                      0 Offline
                      0x01AA
                      wrote on last edited by
                      #10

                      Your first union does the job. So maybe you give "uint8_t byLSBH;"another name to make it clear it is not relevant?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups