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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:20:03+00:00 2026-06-17T08:20:03+00:00

I would like to define a macro that will help me to auto generate

  • 0

I would like to define a macro that will help me to auto generate offsets. Something like this:

#define MEM_OFFSET(name, size) ...

MEM_OFFSET(param1, 1);
MEM_OFFSET(param2, 2);
MEM_OFFSET(param3, 4);
MEM_OFFSET(param4, 1);

should generate the following code:

const int param1_offset = 0;
const int param2_offset = 1;
const int param3_offset = 3;
const int param4_offset = 7;

or

enum {
  param1_offset = 0,
  param2_offset = 1,
  param3_offset = 3,
  param4_offset = 7,
}

or even (not possible using C-preprocessor only for sure, but who knows 😉

#define param1_offset 0
#define param2_offset 1
#define param3_offset 3
#define param4_offset 7

Is it possible to do without running external awk/bash/… scripts?

I’m using Keil C51

  • 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-17T08:20:04+00:00Added an answer on June 17, 2026 at 8:20 am

    In the comments to your post you mention that you’re managing an EEPROM memory map, so this answer relates to managing memory offsets rather than answering your specific question.

    One way to manage EEPROM memory is with the use of a packed struct. ie, one where there is no space between each of the elements. The struct is never instantiated, it is only used for offset calculations.

    typedef struct {
        uint8_t param1;
    #ifdef FEATURE_ENABLED
        uint16_t param2;
    #endif
        uint8_t param3;
    } __packed eeprom_memory_layout_t;
    

    You could then use code like the following to determine the offset of each element as needed(untested). This uses the offsetof stddef macro.

    uint16_t read_param3(void) {
        uint8_t buf;
        eeprom_memory_layout_t * ee;
    
        /* eeprom_read(offset, size, buf) */
        eeprom_read(offsetof(eeprom_memory_layout_t, param3), sizeof(ee->param3), &buf);
    
        return buf;
    }
    

    Note that the struct is never instantiated. Using a struct like this makes it easy to see your memory map at a glance, and macros can easily be used to abstract away the calls to offsetof and sizeof during access.

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

Sidebar

Related Questions

I would like to define a function that will be invoked from entry_32.S. It
I would like to write a macro that will run every time a record
I would like to test bit size of unsigned long using macro. One would
When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) a
I'm a newbie in boost.python and i'm getting this error that I would like
My assert macro is like this: #ifdef DEBUG #define ASSERT(x) ((void)(!(x) && assert_handler(#x, __FILE__,
I am trying to convert a macro in C to something that would work
I would like to export some macros that I have defined and be able
I would like to define the following in Javascript: var tileXml = new Array();
I would like to define a key & value in Settings.settings and bind 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.