0

I'm stumped by the compiler error "error: array must be initialized with a brace-enclosed initializer". None of the other examples of this problem seem to related to this. I haven't touched C in about 14 years, so I think the term "rust" is a bit generous. I'm sure I've just missed something silly.

typedef uint8_t DeviceAddress[8];
DeviceAddress Probe01 = { 0x28, 0xFF, 0x87, 0x5A, 0x91, 0x15, 0x04, 0xE0 }; 
DeviceAddress Probe02 = { 0x28, 0xFF, 0x97, 0x5E, 0x91, 0x15, 0x04, 0x92 };
DeviceAddress Probe03 = { 0x28, 0xFF, 0xCD, 0x81, 0x91, 0x15, 0x01, 0x1E };
DeviceAddress Probe04 = { 0x28, 0xFF, 0xA6, 0x69, 0x91, 0x15, 0x04, 0x15 };
DeviceAddress Probe05 = { 0x28, 0xFF, 0xD8, 0x7E, 0x91, 0x15, 0x04, 0x64 };

struct DeviceInfo {
  DeviceAddress addr;
  const char * name;
};

struct DeviceInfo devices[5] = {
  {.addr = Probe01, .name = "Pump1"},
  {.addr = Probe02, .name = "Pump2"},
  {.addr = Probe03, .name = "Pump3"},
  {.addr = Probe04, .name = "Pump4"},
  {.addr = Probe05, .name = "Pump5"}
};
3
  • 1
    You cannot copy an array through assignment, even if its type is aliased via typedef. So initialiser .addr = Probe01 is not valid. Commented Feb 15, 2016 at 1:37
  • do you need Probe01 etc. to exist as separate arrays besides the devices table or did you just declare them as helpers to make the table tidier? Commented Feb 15, 2016 at 1:46
  • Years of Python and Java have addled my mind (maybe beyond repair). Thank you all for you help. Since there isn't much enhancement to the readability of the code using Probe0X and then pairing that variable with the name I just followed the example: {.addr = { 0x28, 0xFF, 0x87, 0x5A, 0x91, 0x15, 0x04, 0xE0 }, .name = "Pump1" }. Commented Feb 15, 2016 at 18:47

2 Answers 2

3
struct DeviceInfo devices[5] = {
  {.addr = Probe01, .name = "Pump1"},
  {.addr = Probe02, .name = "Pump2"},
  {.addr = Probe03, .name = "Pump3"},
  {.addr = Probe04, .name = "Pump4"},
  {.addr = Probe05, .name = "Pump5"}
};

Here, addr is of type DeviceAddress, which is just a uint8_t array. You can't assign into an array in C, so the compiler is telling you that assigning Probe1 into the field addr is invalid; it needs its own brace-enclosed array initializer there.

You have a couple of options. You could get rid of Probe01, Probe02, etc altogether and just initialize the arrays as the compiler suggests:

struct DeviceInfo devices[5] = {
  {.addr = { 0x28, 0xFF, 0x87, 0x5A, 0x91, 0x15, 0x04, 0xE0 }, .name = "Pump1" },
  ...
};

Another option, which is a bit roundabout, is to have two typedefs:

typedef uint8_t DeviceAddress[8];
typedef uint8_t * DeviceAddress_P;

struct DeviceInfo {
  DeviceAddress_P addr;
  const char * name;
};

And use the pointer type in the struct, initializing it to point to the first element of the array you created:

DeviceAddress Probe01 = { 0x28, 0xFF, 0x87, 0x5A, 0x91, 0x15, 0x04, 0xE0 };

struct DeviceInfo devices[5] = {
  {.addr = Probe01, .name = "Pump1"},
  ...
};

However, this way, the struct just points to an external array, which is probably not desirable.

Sign up to request clarification or add additional context in comments.

Comments

1

If you use "Probe0x" just as helpers to make think clearer as asked by @M.M, you can initialise the array devices of type Struct DeviceInfo as fallow:

struct DeviceInfo devices[5] = {
  {{ 0x28, 0xFF, 0x87, 0x5A, 0x91, 0x15, 0x04, 0xE0 }, "Pump1"},
  {{ 0x28, 0xFF, 0x97, 0x5E, 0x91, 0x15, 0x04, 0x92 }, "Pump2"},
  {{ 0x28, 0xFF, 0xCD, 0x81, 0x91, 0x15, 0x01, 0x1E }, "Pump3"},
  {{ 0x28, 0xFF, 0xA6, 0x69, 0x91, 0x15, 0x04, 0x15 }, "Pump4"},
  {{ 0x28, 0xFF, 0xD8, 0x7E, 0x91, 0x15, 0x04, 0x64 }, "Pump5"}
};

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.