[英]How to define a macro to use with struct array C
I've defined a macro to set my values(C code), for example: 我已经定义了一个宏来设置我的值(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);
but I need to set a "struct array" like: 但我需要设置一个“结构数组”,如:
values_struct pro_struct[10];
and to set default values with a macro, it's possible and how I can do that? 并使用宏设置默认值,这是可能的,我怎么能这样做?
Redefine that macro as 将该宏重新定义为
#define INIT_VALUES_STRUCT {.details = 0x00, .info = { 0x01 } }
And then you can have 然后你就可以拥有
struct values_struct pro_struct = INIT_VALUES_STRUCT;
struct values_struct pro_struct_arr[] = { INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT,
INIT_VALUES_STRUCT };
Why make it complicated with macros when the following works: 以下工作原理时为什么要使宏复杂化:
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.