Thread: C structure help required

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    Lightbulb C structure help required

    U8 --- 1 byte
    s16---2 byte

    typedef struct
    {
    U8 DL ;
    U8 IN ;
    U8/S16 Data ; /* Data*/
    }Appl;

    1.) As u can c the Data field i want to have can be of 1 byte or 2 bytes
    please advice the type of structure i should have i don't want to have 2 structures.

    2.) i want to transmit the above data through the serial port using my driver, now if the Transmission has to be in BIG ENDIAN format the High byte goes first....when i recieve the same structure back how do i know the data is One bye or 2 byte.

    cool

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should either modify your structure to keep an ID flag which denotes the fact that the last variable is to be one byte instead of two, or if your "two byte" range is not using all of the bits, set one bit in the two byte range and check it to see if you're supposed to use one byte or two.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>please advice the type of structure i should have i don't want to have 2 structures.
    Maybe something like this would work for you, the endian flag tells you which member of the union is being used, and the union saves you from having two different memory locations for the variabls DataS and DataU.
    Code:
    typedef struct {
      U8 DL;
      U8 IN;
      unsigned endian: 1;
      union {
        U8  DataU;
        S16 DataS;
      } Data;
    } Appl;
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. structure tag required?
    By barim in forum C Programming
    Replies: 2
    Last Post: 04-26-2004, 10:36 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM