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

  • Home
  • SEARCH
  • 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 8941329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:16:30+00:00 2026-06-15T11:16:30+00:00

Need help with union struct. I’m receiving byte stream that consists of various packets,

  • 0

Need help with union struct. I’m receiving byte stream that consists of various packets, so I’m putting the byte data into union struct and accessing needed data via struct members. The problem is with uint32_t type member – the read skips its two bytes and shows wrong value when accessing via its member. Here’s full demo code:

PacketUtils.h

#include <stdint.h>

typedef struct {

  uint8_t startSymbol;
  uint8_t packetType;
  uint32_t deviceId;
  uint16_t packetCRC;

} PacketData;

typedef union {

  uint8_t *bytes; // stores raw bytes
  PacketData *packet; 

} Packet;

// Puts bytes into predefined struct
void getPacketFromBytes(void *bytes, Packet *packetRef);

PacketUtils.c

#include <stdio.h>
#include "UnionStruct.h"

void getPacketFromBytes(void *bytes, Packet *packetRef)
{
  uint8_t *rawBytes = (uint8_t *)bytes;
  packetRef->bytes = rawBytes;
}

Calling code:

// sample byte data
uint8_t packetBytes[] = {0x11, 0x02, 0x01, 0x01, 0x01, 0x03, 0xbb, 0xbd};

Packet packetRef;
getPacketFromBytes(packetBytes, &packetRef);

printf("%x\n", packetRef.packet->startSymbol); // good - prints 0x11
printf("%x\n", packetRef.packet->packetType); // good - prints 0x02 
printf("%x\n", packetRef.packet->deviceId); // bad - prints bd bb 03 01
printf("%x\n", packetRef.packet->packetCRC); // bad - prints 36 80 (some next values in memory)

Everything is OK when PacketData struct consist of uint8_t or uint16_t type members then the print shows correct values. However, printing deviceId of type uint32_t skips two bytes (0x01 0x01) and grabs last 4 bytes. Printing packetCRC prints the values out of given byte array – some two values in memory, like packetBytes[12] and packetBytes[13]. I can’t figure out why it skips two bytes…

  • 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-15T11:16:31+00:00Added an answer on June 15, 2026 at 11:16 am

    The problem is due to the fields being padded out to default alignment on your platform. On most modern architectures 32-bit values are most efficient when read/written to a 32-bit word aligned address.

    In gcc you can avoid this by using a special attribute to indicate that the structure is “packed”. See here:

    http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Type-Attributes.html

    So struct definition would look something like this:

    typedef struct {
    
        uint8_t startSymbol;
        uint8_t packetType;
        uint32_t deviceId;
        uint16_t packetCRC;
    
    } PacketData __attribute__((packed));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with union two cell arrays. I have: data{1} = 'alon'
Need help with a query that I wrote: I have three tables Company id
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
Need help writing a script downloads data from google insight using c# this is
I need a little help writing a query. I have this data... vDir iNumber
I have a table that I need to get some specific data from for
I've run into an organization problem with windows application form that I need some
What I need to do is export data into CSV file using T-SQL. And
I need to create binary data packets in C code. However, I am missing
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above

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.