如何定义与结构数组 C 一起使用的宏
How to define a macro to use with struct array C
我已经定义了一个宏来设置我的值(C 代码),例如:
.h file
typedef struct {
uint8_t details;
uint8_t info[20];
} values_struct;
#define INIT_VALUES_STRUCT(X) values_struct X = {.details = 0x00, .info = { 0x01 } }
.c file
INIT_VALUES_STRUCT(pro_struct);
但我需要设置一个 "struct array",例如:
values_struct pro_struct[10];
并使用宏设置默认值,这是可能的,我该怎么做?
当以下工作正常时,为什么要使用宏使其复杂化:
#include <stdio.h>
#include <stdint.h>
struct x {
uint8_t details;
uint8_t info[2];
};
int main(void) {
struct x arr[2] = {
{ 1, {5, 6}},
{ 3, {4, 7}}
};
// your code goes here
return 0;
}
将该宏重新定义为
#define INIT_VALUES_STRUCT {.details = 0x00, .info = { 0x01 } }
然后你可以
struct values_struct pro_struct = INIT_VALUES_STRUCT;
struct values_struct pro_struct_arr[] = { INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT };
我已经定义了一个宏来设置我的值(C 代码),例如:
.h file
typedef struct {
uint8_t details;
uint8_t info[20];
} values_struct;
#define INIT_VALUES_STRUCT(X) values_struct X = {.details = 0x00, .info = { 0x01 } }
.c file
INIT_VALUES_STRUCT(pro_struct);
但我需要设置一个 "struct array",例如:
values_struct pro_struct[10];
并使用宏设置默认值,这是可能的,我该怎么做?
当以下工作正常时,为什么要使用宏使其复杂化:
#include <stdio.h>
#include <stdint.h>
struct x {
uint8_t details;
uint8_t info[2];
};
int main(void) {
struct x arr[2] = {
{ 1, {5, 6}},
{ 3, {4, 7}}
};
// your code goes here
return 0;
}
将该宏重新定义为
#define INIT_VALUES_STRUCT {.details = 0x00, .info = { 0x01 } }
然后你可以
struct values_struct pro_struct = INIT_VALUES_STRUCT;
struct values_struct pro_struct_arr[] = { INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT };