在 X-Macro 中打印数组元素时出现编译错误
Compilation error when printing array elements in a X-Macro
我在使用 x-macro 时有以下代码:
#define X_FIELDS \
X(int, var1) \
X(uint8_t, var3) \
X(uint16_t, var4) \
XA(uint8_t, arr1, 4) \
XB(char, arr2, 2)
typedef struct {
#define X(type, name) type name;
#define XA(type, name, count) type name[count];
#define XB(type, name, count) type name[count];
X_FIELDS
#undef X
#undef XA
#undef XB
} myStruct;
以及打印值的函数:
void print(myStruct *aStruct)
{
int i;
#define X(type, name) printf("mystruct.%s is %d\n", #name, aStruct->name);
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", name[i]); \
}
#define XB(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %c\n", name[i]); \
}
X_FIELDS
#undef X
#undef XA
#undef XB
}
和主要功能:
int main() {
myStruct a = {.var1 = 23, .var4 = 12, .arr1 = {32,15,22,11} };
print(&a);
}
不知道为什么,但在编译时,我收到以下错误:
编译器无法看到 arr1 和 arr2,前提是我能够在预处理器文件中看到两者.以下是预处理器的输出:
typedef struct {
int var1; uint8_t var3; uint16_t var4; uint8_t arr1[4]; char arr2[2];
} myStruct;
void print(myStruct *aStruct)
{
int i;
# 44 "C:\Users\akumar8\CodeBlockWorkspace\myTest\myTest\main.c"
printf("mystruct.%s is %d\n", "var1", aStruct->var1); printf("mystruct.%s is %d\n", "var3", aStruct->var3); printf("mystruct.%s is %d\n", "var4", aStruct->var4); for (i=0; i < 4; i++) { printf("element = %u\n", arr1[i]); } for (i=0; i < 2; i++) { printf("element = %c\n", arr2[i]); }
}
知道我做错了什么吗?
正如您在预处理器输出中看到的那样,宏中的 name
的计算结果为 arr1
- 它不知道它是 aStruct
的一部分。
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", name[i]); \
}
对于有效的行,您在宏
中包含了结构 aStruct
#define X(type, name) printf("mystruct.%s is %d\n", #name, aStruct->name);
所以你也应该像这样在这里做同样的事情
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", aStruct->name[i]); \
}
我在使用 x-macro 时有以下代码:
#define X_FIELDS \
X(int, var1) \
X(uint8_t, var3) \
X(uint16_t, var4) \
XA(uint8_t, arr1, 4) \
XB(char, arr2, 2)
typedef struct {
#define X(type, name) type name;
#define XA(type, name, count) type name[count];
#define XB(type, name, count) type name[count];
X_FIELDS
#undef X
#undef XA
#undef XB
} myStruct;
以及打印值的函数:
void print(myStruct *aStruct)
{
int i;
#define X(type, name) printf("mystruct.%s is %d\n", #name, aStruct->name);
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", name[i]); \
}
#define XB(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %c\n", name[i]); \
}
X_FIELDS
#undef X
#undef XA
#undef XB
}
和主要功能:
int main() {
myStruct a = {.var1 = 23, .var4 = 12, .arr1 = {32,15,22,11} };
print(&a);
}
不知道为什么,但在编译时,我收到以下错误:
编译器无法看到 arr1 和 arr2,前提是我能够在预处理器文件中看到两者.以下是预处理器的输出:
typedef struct {
int var1; uint8_t var3; uint16_t var4; uint8_t arr1[4]; char arr2[2];
} myStruct;
void print(myStruct *aStruct)
{
int i;
# 44 "C:\Users\akumar8\CodeBlockWorkspace\myTest\myTest\main.c"
printf("mystruct.%s is %d\n", "var1", aStruct->var1); printf("mystruct.%s is %d\n", "var3", aStruct->var3); printf("mystruct.%s is %d\n", "var4", aStruct->var4); for (i=0; i < 4; i++) { printf("element = %u\n", arr1[i]); } for (i=0; i < 2; i++) { printf("element = %c\n", arr2[i]); }
}
知道我做错了什么吗?
正如您在预处理器输出中看到的那样,宏中的 name
的计算结果为 arr1
- 它不知道它是 aStruct
的一部分。
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", name[i]); \
}
对于有效的行,您在宏
中包含了结构aStruct
#define X(type, name) printf("mystruct.%s is %d\n", #name, aStruct->name);
所以你也应该像这样在这里做同样的事情
#define XA(type, name, count) \
for (i=0; i < count; i++) { \
printf("element = %u\n", aStruct->name[i]); \
}