gpt4 book ai didi

C - 初始化结构中的指针

转载 作者:行者123 更新时间:2023-11-30 15:16:14 25 4
gpt4 key购买 nike

我正在 Arduino 上使用 C 语言工作。我正在尝试初始化结构(链接列表)内的指针。它是一个数据对象,所以我想立即初始化整个对象,而不是稍后在代码中使用 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
};

最佳答案

正如评论中所说 - 您需要指针但提供结构的主要问题,解决此问题的一种变体可能是:

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};

关于C - 初始化结构中的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com