Replace a string in a macro variable?

雨燕双飞 提交于 2020-06-26 13:51:26

问题


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..


回答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.




回答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.



来源:https://stackoverflow.com/questions/4038151/replace-a-string-in-a-macro-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!