Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:57:23+00:00 2026-06-17T06:57:23+00:00

I have the following struct: typedef union { struct { unsigned char ID; unsigned

  • 0

I have the following struct:

typedef union
{
    struct
    {
        unsigned char       ID;
        unsigned short      Vdd;

        unsigned char       B1State;
        unsigned short      B1FloatV;
        unsigned short      B1ChargeV;
        unsigned short      B1Current;
        unsigned short      B1TempC;
        unsigned short      B1StateTimer;
        unsigned short      B1DutyMod;

        unsigned char       B2State;
        unsigned short      B2FloatV;
        unsigned short      B2ChargeV;
        unsigned short      B2Current;
        unsigned short      B2TempC;
        unsigned short      B2StateTimer;
        unsigned short      B2DutyMod;

    } bat_values;
    unsigned char buf[64];
} BATTERY_CHARGE_STATUS;

and I am stuffing it from an array as follows:

for(unsigned char ii = 0; ii < 64; ii++) usb_debug_data.buf[ii]=inBuffer[ii];

I can see that the array has the following (arbitrary) values:

inBuffer[0] = 80;
inBuffer[1] = 128;
inBuffer[2] = 12;
inBuffer[3] = 0;
inBuffer[4] = 23;
...

now I want display these values by changing the text of a QEditLine:

str=QString::number((int)usb_debug_data.bat_values.ID);
ui->batID->setText(str);
str=QString::number((int)usb_debug_data.bat_values.Vdd)
ui->Vdd->setText(str);
str=QString::number((int)usb_debug_data.bat_values.B1State)
ui->B1State->setText(str);
...

however, the QEditLine text values are not turning up as expected. I see the following:

usb_debug_data.bat_values.ID = 80 (correct)
usb_debug_data.bat_values.Vdd = 12 (incorrect)
usb_debug_data.bat_values.B1State = 23 (incorrect)

seems like ‘usb_debug_data.bat_values.Vdd’, which is a short, is not taking its value from inBuffer[1] and inBuffer[2]. Likewise, ‘usb_debug_data.bat_values.B1State’ should get its value from inBuffer[3] but for some reason is picking up its value from inBuffer[4].

Any idea why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:57:24+00:00Added an answer on June 17, 2026 at 6:57 am

    C and C++ are free to insert padding between elements of a structure, and beyond the last element, for whatever purposes it desires (usually efficiency but sometimes because the underlying architecture does not allow unaligned access at all).

    So you’ll probably find that items of two-bytes length are aligned to two-byte boundaries, so you’ll end up with something like:

    unsigned char       ID;          // 1 byte
    //                                  1 byte filler, aligns following short
    unsigned short      Vdd;         // 2 bytes
    unsigned char       B1State;     // 1 byte
    //                                  3 bytes filler, aligns following int
    unsigned int        myVar;       // 4 bytes
    

    Many compilers will allow you to specific how to pack structures, such as with:

    #pragma pack(1)
    

    or the gcc:

    __attribute__((packed))
    

    attribute.

    If you don’t want to (or can’t) pack your structures, you can revert to field-by-filed copying (probably best in a function):

    void copyData (BATTERY_CHARGE_STATUS *bsc, unsigned char *debugData) {
    
        memcpy (&(bsc->ID), debugData, sizeof (bsc->ID));
        debugData += sizeof (bsc->ID);
    
        memcpy (&(bsc->Vdd), debugData, sizeof (bsc->Vdd));
        debugData += sizeof (bsc->Vdd);
    
        : : :
    
        memcpy (&(bsc->B2DutyMod), debugData, sizeof (bsc->B2DutyMod));
        debugData += sizeof (bsc->B2DutyMod); // Not really needed
    
    }
    

    It’s a pain that you have to keep the structure and function synchronised but hopefully it won’t be changing that much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following struct defination: typedef union U08_16_t { unsigned long s32;//message32 unsigned char
I have the following type definition: typedef union{ unsigned int Entry; struct { unsigned
I have defined the following structure below. typedef union { struct { unsigned command:15;
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
I have the following C++ struct: typedef struct FormulaSyntax{ WORD StructSize; short formulaSyntax [2];
I have following structure typedef struct List_Node { struct File_Descriptor *data; char *key; struct
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have the following structure typedef struct DeviceInfo { char[30] name; char[30] serial Number;
I have the following structs: typedef struct bucket { unsigned int contador; unsigned int
I have the following struct typedef char String[256]; typedef struct { String name; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.