[英]C - Initialize a pointer in a struct
I'm working in C on an Arduino. 我在Arduino上使用C语言工作。 I'm trying to initialize a pointer inside a struct (linked list).
我正在尝试初始化结构(链接列表)中的指针。 It's meant to be a data object so I want to initialize the entire object at once rather than using malloc later in the code.
它本来是一个数据对象,所以我想立即初始化整个对象,而不是稍后在代码中使用malloc。
const int PINS_LEN = 20;
struct Command {
float toBrightness; //Percent
float overTime; //Seconds
};
struct CommandNode {
struct Command command;
struct CommandNode *next;
};
struct Sequence {
struct CommandNode commands;
float startTime;
float staggerTime;
int pins[PINS_LEN];
};
struct SequenceNode { //Pattern
struct Sequence sequence;
struct SequenceNode *next;
};
struct SequenceNode pattern = {
.sequence = {
.commands = {
.command = {
.toBrightness = 100,
.overTime = 1.0
},
//-=-=-=THIS IS WHERE IT DIES=-=-=-
.next = {
.command = {
.toBrightness = 50,
.overTime = 0.5
},
.next = NULL
},
//-=-=-=-=-=-=-=-=-=-=
},
.startTime = 0.0,
.staggerTime = 1.0,
.pins = {0, 1, 2, 3, 4, 5}
},
.next = NULL
};
as it was said in comments - the main problem that you need pointer, but provide struct, one variant to work around this could be: 正如评论中所说的那样-主要问题是您需要指针,但要提供结构,可以解决此问题的一种变体可能是:
struct CommandNode next = {.command = {.toBrightness = 50, .overTime = 0.5}, .next = NULL};
struct SequenceNode pattern = {.sequence = {
.commands = {
.command = {.toBrightness = 100, .overTime = 1.0},
.next = &next},
.startTime = 0.0,
.staggerTime = 1.0,
.pins = {0, 1, 2, 3, 4, 5}
},
.next = NULL};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.