我定义了一个struct,有一个成员类型是uint8_t,这个成员存储mac地址。
在 Arduino IDE 上编程。
结构:
typedef struct devInfo{
uint8_t address[];
unsigned int count;
unsigned int filePos;
}struct_devInfo;
我用这两种方式给一个值,但不能给变量赋值。
方法一> struct_devInfo slave = {{0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6}, 0, 0};
方法二> slave.address[] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6};
如何访问这种类型的值?
我将使用这个结构的变量,salve1,slave2...等。
除了结构,还有什么更好的方法吗?你能演示一下吗?
具有灵活数组成员的结构需要是结构中的最后一个成员。更改为
typedef struct devInfo{
unsigned int count;
unsigned int filePos;
uint8_t address[];
}struct_devInfo;
然后你需要分配足够的内存:
struct_devInfo *p = malloc(sizeof *p + size);
那么你可以这样做:
const uint8_t initArr[] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6};
memcpy(p, initArr, sizeof initArr);
但由于它似乎是一个不需要灵活成员的字段,因此我会使用它:
typedef struct devInfo{
unsigned int count;
unsigned int filePos;
uint8_t address[6]; // Specify the size
}struct_devInfo;
那么你就不需要分配内存
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句