我已经定义了一个宏来设置我的值(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];
并使用宏设置默认值,这是可能的,我该怎么做?
答案 0 :(得分:4)
将该宏重新定义为
#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 };
答案 1 :(得分:1)
为什么在以下工作时使宏复杂化:
#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;
}