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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:24:16+00:00 2026-05-13T19:24:16+00:00

Forgive me if this comes across as a trivial question – I’m usually a

  • 0

Forgive me if this comes across as a trivial question – I’m usually a control systems guy (plc’s and automation) but have found myself involved in some embedded micro-controller and PC projects lately.

Let’s say I have a function that accepts a pointer to an array of ‘command bytes’, typically 5 or 10 bytes in length, like so:

char cmd_i2c_read(unsigned char *cmd, unsigned short cmd_len) { ... }

I want to decode the command bytes (*cmd).

Is it better form to:

  1. Create local variables indicating the purpose of each byte:

    unsigned char device_address = cmd[2];
    unsigned char register_address = cmd[3];
    unsigned char num_bytes = cmd[4];
    // use the local variables: if(num_bytes &le 0xFF) { do_stuff(device_address, register_address, num_bytes); }
  2. Create local pointers:

    unsigned char *device_address = &cmd[2];
    unsigned char *register_address = &cmd[3];
    unsigned char *num_bytes = &cmd[4];
    // use the pointers: if(*num_bytes &le 0xFF) { do_stuff(*device_address, *register_address, *num_bytes); }
  3. Index the *cmd array directly:
    if(cmd[4] <= 0xFF) {
    do_stuff(cmd[2], cmd[3], cmd[4]);
    }

  • 1 1 Answer
  • 4 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-05-13T19:24:16+00:00Added an answer on May 13, 2026 at 7:24 pm

    Option 1 is clear but a bit wordy. I don’t like 2 at all, and 3 is hard to understand. Personally I prefer to use structs for this sort of thing.

    typedef struct  {
       unsigned char whatever[2];
       unsigned char device_address;
       unsigned char register_address;
       unsigned char num_bytes;
       }  CMD;
    
    CMD * pcmd = (CMD *)&cmd[0];
    
    // use the local variables:
    if(num_bytes ≤ 0xFF) {
        do_stuff(pcmd->device_address, pcmd->register_address, pcmd->num_bytes);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Forgive me if this is a repeated question but I am having trouble finding
First off, please forgive the stupidness of this question but Im not from a
I'm a novice when it comes to sql so forgive me if this seems
I'm very much a newbie when it comes to all this so forgive my
Forgive if this question is silly. I just started reading the SugarCRM developer documentation
Forgive me for this unbeautiful code, but I'm trying to get it to work
Forgive me if this has been asked before, but I assure you I've scoured
Forgive me if this is a repeat question. I've searched StackOverflow and did not
Forgive me if this is a naïve question, however I am at a loss
I'm kinda new to rails so forgive if this is a stupid question. The

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.