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 8415201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:22:34+00:00 2026-06-10T01:22:34+00:00

I have a C program that receives a 64 byte array of char (which

  • 0

I have a C program that receives a 64 byte array of char (which is passed via USB). Depending on the first byte (which indicates the command type) I want to ‘impose’ a structure over the char array to make the code clearer.

For example, if the command code is 10 I would expect something like:

struct
{
    uint8_t commandNumber;
    uint16_t xPos;
    uint16_t yPos;
    int32_t identificationNumber;
 } commandTen;

So I would like to cast my char packet[64] ‘onto’ commandTen and then access the fields using something like:

localVar = commandTenPacket->xPos;

How can this be achieved in C?

Thanks in advance!

  • 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-10T01:22:36+00:00Added an answer on June 10, 2026 at 1:22 am

    First, as others said you’d have to ensure that your struct has no padding. Your compiler probably has an extension for that, #pragma pack or so.

    Define a struct for each of your use cases, not a variable as in your example.

    Then define a union

    typedef union overlay overlay;
    
    union overlay {
     uint8_t raw[64];
     struct type10 command10;
     struct type42 command42;
    };
    

    Now create a buffer of that type

    overlay buffer;
    

    Feed the “raw” part to your function that receives the data: getit(buffer.raw). And then

    switch (buffer.raw[0]) {
     case 10: {
       // use buffer.command10
       break;
     }
     case 42: {
       // use buffer.command42
     }
    }
    

    This is guaranteed by the C standard to work well, since you are reading everything as uint8_t AKA unsigned char. In fact, the principle use case for unions is just this sort of “type puning”. The whole network layer with it different types of IPv4, IPv6 etc addresses works in a similar way.

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

Sidebar

Related Questions

I have a c++/windows program that receives data from another c++ program via a
I have a program that receives real time data on 1000 topics. It receives
i have a program that receives data from a wireless device over bluetooth...i now
I have a program that receives webcam video and displays it on a C#
I have a program with several worker threads, and a main thread that receives
I have a program that receives input arguments: $ myProgram.exe -arg1 -arg2 -arg3 in
I have a program that receives serial data and uses matplotlib to graph it
I have a simple RabbitMQ test program that sends and then receives a message
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it

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.