是否有可能改变这种X宏构建一个结构,它包括阵列?怎么样? [英] Is it possible to modify this X-Macro to build a struct, which includes arrays? How?
问题描述
发现这个非常有用的Q / A对SO:是否有通过在C diferent类型的元素一个struct任何方式循环?
但因为我很新的,整个的X宏观的东西,我想知道,如果它怎么有可能去适应这个例子用数组一个结构 - 是这样的:
typedef结构
{
UINT8地址1 [SIZEOF_ADDR]
UINT8地址2 [SIZEOF_ADDR]
UINT8 ADDR3 [SIZEOF_ADDR]
} TEST;
这是什么会适应:
// ---首先描述结构,田间地头,他们的类型以及如何将它们打印出来
#定义X_FIELDS \\
X(INT,FIELD1,%D)\\
X(INT,场2,%D)\\
X(CHAR,FIELD3,%C)\\
X(字符*,字段4,%S)// ---定义结构中,X宏将每场一次膨胀
typedef结构{
#定义X(类型,名称,格式)键入名称;
X_FIELDS
和#undef点¯x
} MYSTRUCT;
我的出发点是这样,但我敢肯定,格式必须是别的东西,或将不得不被替换:
的#define X_FIELDS \\
X(UINT8,地址1%02X)\\
X(UINT8,地址2,%02X)\\
X(UINT8,ADDR3,%02X)
这是地址会是这样{} 0x10,0x12,0x0A - 大小相同的
的编辑:的
这是一个例子,我如何使用这个结构目前,没有X-宏:
TEST测试= {{} 0x16,0xA4,0x3,
{} 0x16,0xA4,0x2,
{0x16,0xA4,0x1}};的printf(%02X,test.addr1 [I]);
您已经有了一个良好的开端...
的#include<&stdio.h中GT;
的typedef unsigned char型UINT8;
枚举{SIZEOF_ADDR = 3};#定义X_FIELDS \\
X(UINT8,地址1,为0x%02X)\\
X(UINT8,地址2,为0x%02X)\\
X(UINT8,ADDR3,为0x%02X)// ---定义结构中,X宏将每场一次膨胀
typedef结构{
#定义X(类型,名称,格式)类型名称[SIZEOF_ADDR]
X_FIELDS
和#undef点¯x
} TEST;EXTERN无效iterate1(TEST *测试);
EXTERN无效iterate2(TEST *测试);// ---打印的值
无效iterate1(TEST *测试)
{
为const char *垫;
// ---迭代过结构的所有领域
#定义X(类型,名称,格式)\\
的printf(%s是#NAME); \\
垫={; \\
用于(为size_t我= 0; I<的sizeof(测试 - >名);我+ +)\\
{\\
的printf(%S格式,垫,测试 - >名[I]); \\
垫=,; \\
} \\
的printf(} \\ n);
X_FIELDS
和#undef点¯x
}//或者,定义一个函数`print_addr()`
静态无效print_addr(为const char *格式,常量UINT8 *地址,为size_t addrsize)
{
焦炭垫='{';
用于(为size_t我= 0; I< addrsize;我++)
{
的putchar(垫);
的printf(格式,地址[I]);
垫=',';
}
的putchar('}');
}// ---打印使用print_addr值()
无效iterate2(TEST *测试)
{
// ---迭代过结构的所有领域
#定义X(类型,名称,格式)\\
的printf(%s是#NAME); \\
print_addr(格式,测试 - >名,sizeof的(测试 - >名)); \\
的printf(\\ n);
X_FIELDS
和#undef点¯x
}
(这code,作为一个单一的文件处理,是众所周知的GCC 4.7.1下顺利地编译在Mac OS X 10.7.5。)
Found this very helpful Q/A on SO: Is there any way to loop through a struct with elements of diferent types in C?
but since I am quite new to the whole X-Macro stuff, I was wondering, if and how would it be possible to adapt this example for a struct with arrays - like this:
typedef struct
{
uint8 Addr1[SIZEOF_ADDR];
uint8 Addr2[SIZEOF_ADDR];
uint8 Addr3[SIZEOF_ADDR];
} TEST;
This is what would be to adapt:
//--- first describe the structure, the fields, their types and how to print them
#define X_FIELDS \
X(int, field1, "%d") \
X(int, field2, "%d") \
X(char, field3, "%c") \
X(char *, field4, "%s")
//--- define the structure, the X macro will be expanded once per field
typedef struct {
#define X(type, name, format) type name;
X_FIELDS
#undef X
} mystruct;
My starting point was like this, but I am quite sure, format would have to be something else, or would have to be replaced:
#define X_FIELDS \
X(uint8, Addr1, "%02X") \
X(uint8, Addr2, "%02X") \
X(uint8, Addr3, "%02X")
An address would be something like {0x10,0x12,0x0A} - all of the same size.
Edit:
This is an example how I am using this struct currently, without x-macros:
TEST test = {{0x16,0xA4,0x3},
{0x16,0xA4,0x2},
{0x16,0xA4,0x1}};
printf("%02X",test.addr1[i]);
You've got a reasonable start...
#include <stdio.h>
typedef unsigned char uint8;
enum { SIZEOF_ADDR = 3 };
#define X_FIELDS \
X(uint8, Addr1, "0x%02X") \
X(uint8, Addr2, "0x%02X") \
X(uint8, Addr3, "0x%02X")
//--- define the structure, the X macro will be expanded once per field
typedef struct {
#define X(type, name, format) type name[SIZEOF_ADDR];
X_FIELDS
#undef X
} TEST;
extern void iterate1(TEST *test);
extern void iterate2(TEST *test);
//--- Print the values
void iterate1(TEST *test)
{
const char *pad;
//--- "iterate" over all the fields of the structure
#define X(type, name, format) \
printf("%s is ", #name); \
pad = "{"; \
for (size_t i = 0; i < sizeof(test->name); i++) \
{ \
printf("%s" format, pad, test->name[i]); \
pad = ","; \
} \
printf("}\n");
X_FIELDS
#undef X
}
// Alternatively, define a function `print_addr()`
static void print_addr(const char *format, const uint8 *addr, size_t addrsize)
{
char pad = '{';
for (size_t i = 0; i < addrsize; i++)
{
putchar(pad);
printf(format, addr[i]);
pad = ',';
}
putchar('}');
}
//--- Print the values using print_addr()
void iterate2(TEST *test)
{
//--- "iterate" over all the fields of the structure
#define X(type, name, format) \
printf("%s is ", #name); \
print_addr(format, test->name, sizeof(test->name)); \
printf("\n");
X_FIELDS
#undef X
}
(This code, treated as a single file, is known to compile cleanly under GCC 4.7.1 on Mac OS X 10.7.5.)
这篇关于是否有可能改变这种X宏构建一个结构,它包括阵列?怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!