Defining a Union - A Question of Style

This is a discussion on Defining a Union - A Question of Style within the C Programming forums, part of the General Programming Boards category; Hi All, Please excuse my profound ignorance and inevitable lack of tact. I have a simple question about defining a ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Winslow, Maine
    Posts
    4

    Defining a Union - A Question of Style

    Hi All,

    Please excuse my profound ignorance and inevitable lack of tact. I have a simple question about defining a structure/union. Is there a more elegant, possibly simpler way to achieve the following?

    Code:
    typedef unsigned char       BYTE;     // 8-bit unsigned
    typedef unsigned short int  WORD;     // 16-bit unsigned
    
    typedef struct{
      BYTE month;
      BYTE day;
      BYTE hour;
      BYTE minute;
      BYTE second;
    }sTimeStamp;
    
    typedef struct{
      WORD  resolution  :4;
      WORD  temperature :12;
    }sReading;
      
    typedef struct{
      sTimeStamp  time;
      sReading    sensor1;
      sReading    sensor2;
      sReading    sensor3;
      sReading    sensor4;
    }sSample;
    
    typedef union{
      sSample   elements;
      BYTE      bytes[sizeof(sSample)];
    }uSample;
    The goal is to end up with a union allowing the same data to be referenced in different contexts - as in discrete elements or as a data buffer.
    This method strikes me as a bit verbose, but fairly comprehensible.

    Thanks for any input.
    Last edited by clark.leach; 11-20-2008 at 12:36 AM. Reason: Posted prematurely by accident

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Next time, you might want to write your entire post out in a text editor instead of posting it halfway and then going back to edit it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Winslow, Maine
    Posts
    4
    Yeah. That was an oopsy. Never posted in this forum before and hit return at the wrong time.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > 8-bit unsigned
    > 16-bit unsigned
    Who says they have that many bits!? Look at limits.h where they're defined on a case-by-case basis.

    A union will mean one or the other, not both.
    Last edited by zacs7; 11-20-2008 at 01:57 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by clark.leach View Post
    The goal is to end up with a union allowing the same data to be referenced in different contexts - as in discrete elements or as a data buffer.
    This method strikes me as a bit verbose, but fairly comprehensible.
    One question: Why do you need it as a data buffer? For most intents and purposes, I'd expect that just casting the address of the struct to BYTE * or unsigned char * will be sufficient if you need to have it as a raw buffer (e.g. for disk writes/reads or sending across a network [beware of byte order differences in both cases]).

    As far as I can see, there's nothing wrong with this method. Not sure how you could make it smaller (except for declaring all the sub-structs inside the union, but that would only save a couple of lines and make it MUCH harder to follow).

    Of course this:
    Code:
      sReading    sensor1;
      sReading    sensor2;
      sReading    sensor3;
      sReading    sensor4;
    I probably want to write as:
    Code:
      sReading    sensor[4];
    ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Winslow, Maine
    Posts
    4
    Quote Originally Posted by zacs7 View Post
    > 8-bit unsigned
    > 16-bit unsigned
    Who says they have that many bits!?
    Microchip does, actually. I'm programming one of their microcontrollers.

    Quote Originally Posted by zacs7 View Post
    A union will mean one or the other, not both.
    Yes, that is the definition of a union. I'm not trying to store two things at the same time. I'm simply trying to access the same data different ways.

    Thanks for your input.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Winslow, Maine
    Posts
    4
    Quote Originally Posted by matsp View Post
    One question: Why do you need it as a data buffer? For most intents and purposes, I'd expect that just casting the address of the struct to BYTE * or unsigned char * will be sufficient if you need to have it as a raw buffer (e.g. for disk writes/reads or sending across a network [beware of byte order differences in both cases]).
    You guessed it. I want it to look like a data buffer for transmitting accross a network. I had tried typecasting in an earlier incarnation of this project on a different platform with much confusion and little success. If the net result is the same I think I prefer defining a union, if simply for my peace of mind. Obviously I still have a lot to learn; that's why I'm here

    Quote Originally Posted by matsp View Post
    As far as I can see, there's nothing wrong with this method. Not sure how you could make it smaller (except for declaring all the sub-structs inside the union, but that would only save a couple of lines and make it MUCH harder to follow).
    I suspected as much.

    Quote Originally Posted by matsp View Post
    Of course this:
    Code:
      sReading    sensor1;
      sReading    sensor2;
      sReading    sensor3;
      sReading    sensor4;
    I probably want to write as:
    Code:
      sReading    sensor[4];
    ?
    Yeah; I'm not sure why I ended up with that construct.

    Thanks for your assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21