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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:41:27+00:00 2026-05-30T06:41:27+00:00

I am writing some hardware specific code, where I want to use C Macros,

  • 0

I am writing some hardware specific code, where I want to use C Macros, the macro definition would be something like this:-

#define VALIDATE_RESOURCE_AND_ALLOCATE(MODE,RESOURCE1) if(a[MODE][RESOURCE1] != x1) || \
                                                               (a[MODE][RESOURCE1] != y1)) \
                                                         a[MODE][RESOURCE1]=x3;

Since sometimes I can have more then 1 resource to allocate, such as:-

#define VALIDATE_RESOURCE_AND_ALLOCATE_1(MODE,RESOURCE1,RESOURCE2) if(a[MODE][RESOURCE1] != x1) || \
                                                               (a[MODE][RESOURCE1] != y1)) \
                                                                         a[MODE][RESOURCE1]=x3;
                                                        if(a[MODE][RESOURCE2] != x1) || \
                                                               (a[MODE][RESOURCE2] != y1)) \
                                                         a[MODE][RESOURCE2]=x3;

Is there any way I can write a macro, which covers both cases, as it takes variable number of arguments?

I have used variable number of arguments, in macro for printf macros, but then how I will address those arguments, by their respective name, for example, if I modify the MACRO definition such as:0-

#define VALIDA_RESOURCE_AND_ALLOCATE(MODE,.....) 

How will I identify RESOURCE1, RESOURCE2?

  • 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-30T06:41:29+00:00Added an answer on May 30, 2026 at 6:41 am

    Your macros have a lot of repeated code in them. Simplifying them helps make a solution more apparent:

    #define VALIDATE_RESOURCE_AND_ALLOCATE_1(MODE,RESOURCE1,RESOURCE2) do {\
        VALIDATE_RESOURCE_AND_ALLOCATE(MODE, RESOURCE1); \
        VALIDATE_RESOURCE_AND_ALLOCATE(MODE, RESOURCE2); \
    } while(0)
    

    Here, it’s clearer that this is simply a repeated invocation of the first macro while iterating through a list of arguments.

    Assuming you know that the data types being used here will always be consistent, you can try something like this (untested and written off of the top of my head):

    #ifdef HARDWARE_PLATFORM_A
      static sometype args[] = {
          RESOURCE1,
          RESOURCE2,
          /* ... etc, etc */
      };
    #elif defined HARDWARE_PLATFORM_B
      static sometype args[] = {
          RESOURCE10,
          RESOURCE11,
          /* ... etc, etc */
      };
    /* repeat for all hardware platforms */
    #endif
    
    void initialization_function (void) {
        int i;
        for (i = 0; i < (sizeof(args) / sizeof(args[0])); ++i) {
            VALIDATE_RESOURCE_AND_ALLOCATE(MODE, args[i]);
        }
    }
    

    where sometype is the data type of the arguments that you will be using for RESOURCE1, RESOURCE2, etc.

    Given the complexity of what you are trying to do, you’d be a lot better off writing a function to do the iteration instead of a macro. You can still use a macro to create the RESOURCE list, but don’t try to get the pre-processor to do the iteration for you. If you need to avoid the overhead of a function call (since you tagged this as ’embedded’), you can declare the functions inline and the result should be as efficient as using a macro. In the process, though, you’ll gain things like type safety.

    While it might be technically possible to do this with a macro, it would be a nasty hack that would most likely bring more problems than benefits. Doing complex procedural tasks with the pre-processor rarely turns out well.

    The other alternative that you have is to use a code generator that takes a list of RESOURCE arguments from a file and generates a .c file containing the initialization code. The code generator would be written in a language much more powerful than the C pre-processor (almost any scripting language could be used here). This probably wouldn’t be worth the trouble unless you had a long list of RESOURCEs, though.

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

Sidebar

Related Questions

So I was writing some code today that basically looks like this: string returnString
Say I'm writing some ruby code and I want to use the standard Date
I'm writing some semi-portable code and want to be able to detect when I'm
I'm writing some code to interface with a piece of hardware. The hardware connects
Does the latter deprecate the former? I'm writing code which I would like to
Writing some test scripts in IronPython, I want to verify whether a window is
While writing some C code, I decided to compile it to assembly and read
I am writing some code to determine whether a network domain is registered. For
We are writing an image processing algorithm targeting some Intel hardware. Generally we prefer
I'm writing some code to handle WM_GESTURE and WM_TOUCH events in Windows 7, but

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.