0

I have different Address in Macro's. Which I need to pick any of the address depends on my application. Here the Details below.

#define Location1_Subset1_Sub1          0x011F
#define Location1_Subset1_Sub2          0x0150
#define Location1_Subset1_Sub3          0x0170
#define Location1_Subset2_Sub1          0x0190
#define Location1_Subset2_Sub2          0x01AF
#define Location1_Subset2_Sub3          0x01EF
#define Location2_Subset1_Sub1          0x0211
#define Location2_Subset1_Sub2          0x0230
#define Location2_Subset1_Sub3          0x0240
#define Location2_Subset2_Sub1          0x027F
#define Location2_Subset2_Sub2          0x02A0
#define Location2_Subset2_Sub3          0x02EF

The above Macros is for Address.

if(cond)
{
    var1 = 1;
    if(cond)
    {
        var2 = 2;
    }
    if(cond)
    {
        var3 = 1;
    }
}
uint32 = Read_Address = fn(var1, var2, var3);

This is an example of my application. Based on the var1, var2 and var3, macro should pick the respective address. According to example above. It should pick the Address Location1_Subset2_sub1.

I need to define one macro, which will concatenate the variable. I tried with below macro, which is not right.

#define fn(var1,var2,var3)    (Location##var1_Subset##var2_sub##var3)

It is concat the string "Locationvar1_Subsetvar2_subvar3". But I want which will concate the value in var's. I Would be thankful, if some one guide me.

3
  • 1
    For this you have to use sprintf(buffer,"Location%d_Subset%d_sub%d",var1,var2,var3). Commented Sep 20, 2014 at 8:26
  • Do not write code that is difficult to read and maintain. Macros are a bad idea for complex stuff Commented Sep 20, 2014 at 8:26
  • If location, subset etc are known at compile time, then you shouldn't use variables to store these values but macros. Macros you then can concatenate with the idea you have. Commented Sep 20, 2014 at 10:12

2 Answers 2

3

Macros and variables live in entirely different worlds: they cannot read the value of variables. Macros are expanded during the preprocessing stage, so your program isn't even compiled yet. They can only do purely textual manipulation of your source code.

Consider storing your constants in a static array:

static const uint32 fn[2][2][3] = {
    {
        {0x011F, 0x0150, 0x0170},
        {0x0190, 0x01AF, 0x01EF}
    },
    /* ... */
};

Then you can access them directly with var1 to var3 as indices:

uint32 Read_Address = fn[var1 - 1][var2 - 1][var3 - 1];
Sign up to request clarification or add additional context in comments.

3 Comments

According to you I need to create static array for each combination like 111,112, 113,211, .......523. This is bad idea. That's why I thought to have single macro. which can do the thing for me. Otherwise, I can do it in if and else statement itself.
@Pinkypatel, if your array is really static, and your constants for the cases analysis are really compile time constants, a modern optimizing compiler could be able to eliminate the array completely.
Unless you have a way to compute the values with some direct algorithm, you're out of luck. The program has to get those values from somewhere, static array or otherwise. Even if it were possible to use macros, the values would still have to get loaded into memory at some point during execution. Static arrays may not be space-optimal if the data is sparse though, in which case a conditional block may serve your purposes better. You can use macros to reduce the repetitiveness of the conditional block though, but not by token pasting.
0

Use this source code to concat the strings.

#define fn(var1,var2,var3)    (Location##var1##_Subset##var2##_sub##var3)

But in your program, you can't do through this way.Becase Macro is processed in pre-compile time,not in running time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.