Is it possible to modify this X-Macro to build a s

2019-03-05 10:23发布

Found this very helpful Q/A on SO: Is there any way to loop through a struct with elements of different 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]);

1条回答
太酷不给撩
2楼-- · 2019-03-05 11:02

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

查看更多
登录 后发表回答