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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:50+00:00 2026-05-28T21:26:50+00:00

I’m inexperienced with C, and working on a microcontroller with messages stored in arrays

  • 0

I’m inexperienced with C, and working on a microcontroller with messages stored in arrays where each byte does something different. How do I give each element of the array a human-readable name instead of referencing them as msg[1], msg[2], etc.?

Is this what structs are for? But “you cannot make assumptions about the binary layout of a structure, as it may have padding between fields.”

Should I just use macros like this? (I know “macros are bad”, but the code is already full of them)

#define MSG_ID       msg[0]
#define MSG_COMMAND  msg[1]

Oh! Or I guess I could just do

MSG_ID = 0;
MSG_COMMAND = 1;
MSG[MSG_ID];

That’s probably better, if a little uglier.

  • 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-05-28T21:26:50+00:00Added an answer on May 28, 2026 at 9:26 pm

    If you want to go that route, use a macro, for sure, but make them better than what you suggest:

    #define MSG_ID(x)      (x)[0]
    #define MSG_COMMAND(x) (x)[1]
    

    Which will allow the code to name the arrays in ways that make sense, instead of ways that work with the macro.

    Otherwise, you can define constants for the indexes instead (sorry I could not come up with better names for them…):

    #define IDX_MSG_ID      0
    #define IDX_MSG_COMMAND 1
    

    And macros are not bad if they are used responsibly. This kind of “simple aliasing” is one of the cases where macros help making the code easier to read and understand, provided the macros are named appropriately and well documented.

    Edit: per @Lundin’s comments, the best way to improve readability and safety of the code is to introduce a type and a set of functions, like so (assuming you store in char and a message is MESSAGE_SIZE long):

    typedef char MESSAGE[MESSAGE_SIZE];
    char get_message_id(MESSAGE msg) { return msg[0]; }
    char get_message_command(MESSAGE msg) { return msg[1]; }
    

    This method, though it brings some level of type safety and allows you to abstract the storage away from the use, also introduces call overhead, which in microcontroller world might be problematic. The compiler may alleviate some of this through inlining the functions (which you could incentize by adding the inline keyword to the definitions).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.