Replace a string in a macro variable?

11,105

Solution 1

You haven't found a way to rewrite arbitrary strings because macros cannot do that. Macros names have to be valid identifiers, which -> is not. The C preprocessor is very limited in what it can do. You could look into m4 for a stronger preprocessor, but you're likely headed down the wrong road.

Solution 2

I don't know of any preprocessor mechanism to treat a struct->element parameter as two separate tokens or to autoconvert the -> to an underscore. My suggestion is to have a separate macro, e.g. DO_X2(struct_ptr, element), which would add the "->" or "_" where needed. Then you could use DO_X or DO_X2 as appropriate.

There is a separate problem if you plan to use these macros as indicated. The inner for-loop can declare the exact same variable name and it will not be considered an error since they have different scope. For example, assuming your C compiler supports declaring the iterator within the for statement like this (which I don't believe is standard behavior for C):

for (int i=0; i<10; ++i)

Then you could do the following and it would not be considered an error:

int sum = 0;
for (int i=0; i<10; ++i)
    for (int i=0; i<10; ++i)
        ++sum;

The two "int i" have different scope so it should compile and execute just fine.

Share:
11,105
Brendan Long
Author by

Brendan Long

Updated on June 04, 2022

Comments

  • Brendan Long
    Brendan Long almost 2 years

    I have a macro where I pass in an argument and use that define a new variable based on the name of the input:

    #define DO_X(x) char _do_x_var_ ## x; /* other things */
    

    The problem is if I pass in a struct variable, it breaks:

    DO_X(some_struct->thing)
    

    becomes:

    char _do_x_var_some_struct->thing; /* other things */
    

    EDIT: What I want it to evaluate to is:

    char _do_x_var_some_struct__thing; /* other things */
    

    (or any valid variable name containing something similar to the input)

    What I actually want is for these to work:

    #define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
    DO_X(x){
        DO_X(y) {
            /*things*/
        }
    }
    
    DO_X(object->x){
        DO_X(object->y) {
            /*things*/
        }
    }
    

    but for these to fail:

    #define DO_X(x) for(char _do_x_var_ ## x; /*things*/)
    DO_X(x){
        DO_X(x) { // <-- multiple definition of _do_x_var_x
            /*things*/
        }
    }
    
    DO_X(object->x){
        DO_X(object->x) { // <-- multiple definition of _do_x_var_object__x (or whatever)
            /*things*/
        }
    }
    

    Is there some way to make this work? Maybe replacing -> with __ or something? I've found ways to concatenate, but not replace strings..