0

I'm working in C on an Arduino. 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.

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
};
2
  • 1
    What do you mean by "it dies"? Commented Oct 16, 2015 at 19:49
  • 1
    You cannot initialize like that... you're trying to initialize a pointer to be equal to a struct. You probably thought you could do that because a similar pattern works for C strings, but it's very specialized there. You're going to need to initialize your pointer to point to a concrete location, probably at runtime rather than compile time. Commented Oct 16, 2015 at 19:56

1 Answer 1

1

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};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.