C 宏扩展不起作用但写该行显式有效
C macro expansion not working but writing that line explicitly worked
//#define newScope(string, scopeType) ({ \
intrprtr.scope = realloc(intrprtr.scope, intrprtr.scope_layer*sizeof(struct Scope)); \
strncpy(intrprtr.scope[intrprtr.scope_layer-1].string, (string), 255); \
intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType); \
})
//newScope(string, objScope); // <-- wanted to use macro but it doesn't work. I don't get it
intrprtr.scope = realloc(intrprtr.scope, intrprtr.scope_layer*sizeof(struct Scope));
strncpy(intrprtr.scope[intrprtr.scope_layer-1].string, string, 255);
intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope; // <-- this worked
我希望代码更具可读性,所以我使用了一个宏来包装我们的代码。如果我使用宏,我会得到以下错误。
src/parser.c:114:30: error: no member named 'objScope' in 'struct Scope'
newScope(string, objScope); // <-- wanted to use macro but it doesn't work. I don't get it
~~~~~~~~~~~~~~~~~^~~~~~~~~
src/parser.c:109:48: note: expanded from macro 'newScope'
intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType); \
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
如果我明确地编写代码,我没有得到这个错误。
这是 intrprtr 的声明(在 parser.h 中,我包含在 parser.c 中)
typedef struct{
FILE* file;
char cur_file_name[255];
int line_num;
struct Scope {
char string[255];
enum ScopeType { msgScope, objScope, codeBlock, func } scopeType;
}*scope;
int scope_layer;
}Intrprtr;
extern Intrprtr intrprtr;
据我了解,当我将objScope
传递给newScope
宏的第二个参数时,scopeType
应该扩展为objScope
。 IE。
intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope;
没有成员错误对我来说没有任何意义,尤其是当 intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope;
实际工作时。有人可以为我解释一下吗?谢谢
在替换标记 intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType);
中,scopeType
标记被相应的宏参数替换,objScope
,它出现了两次,但您希望第一个是文字成员名称 scopeType
,不可替换。
将宏参数名称更改为替换标记中未使用的名称。
//#define newScope(string, scopeType) ({ \
intrprtr.scope = realloc(intrprtr.scope, intrprtr.scope_layer*sizeof(struct Scope)); \
strncpy(intrprtr.scope[intrprtr.scope_layer-1].string, (string), 255); \
intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType); \
})
//newScope(string, objScope); // <-- wanted to use macro but it doesn't work. I don't get it
intrprtr.scope = realloc(intrprtr.scope, intrprtr.scope_layer*sizeof(struct Scope));
strncpy(intrprtr.scope[intrprtr.scope_layer-1].string, string, 255);
intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope; // <-- this worked
我希望代码更具可读性,所以我使用了一个宏来包装我们的代码。如果我使用宏,我会得到以下错误。
src/parser.c:114:30: error: no member named 'objScope' in 'struct Scope'
newScope(string, objScope); // <-- wanted to use macro but it doesn't work. I don't get it
~~~~~~~~~~~~~~~~~^~~~~~~~~
src/parser.c:109:48: note: expanded from macro 'newScope'
intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType); \
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
如果我明确地编写代码,我没有得到这个错误。 这是 intrprtr 的声明(在 parser.h 中,我包含在 parser.c 中)
typedef struct{
FILE* file;
char cur_file_name[255];
int line_num;
struct Scope {
char string[255];
enum ScopeType { msgScope, objScope, codeBlock, func } scopeType;
}*scope;
int scope_layer;
}Intrprtr;
extern Intrprtr intrprtr;
据我了解,当我将objScope
传递给newScope
宏的第二个参数时,scopeType
应该扩展为objScope
。 IE。
intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope;
没有成员错误对我来说没有任何意义,尤其是当 intrprtr.scope[intrprtr.scope_layer-1].scopeType = objScope;
实际工作时。有人可以为我解释一下吗?谢谢
在替换标记 intrprtr.scope[intrprtr.scope_layer-1].scopeType = (scopeType);
中,scopeType
标记被相应的宏参数替换,objScope
,它出现了两次,但您希望第一个是文字成员名称 scopeType
,不可替换。
将宏参数名称更改为替换标记中未使用的名称。