STRUCT - how to work with array in sruct

Hi, I'm C++ begginer. I would like to use struct of record. After that I need print array of struct to Serial like that:

0, ID=0x10,0x23,0x94,0x3A
0, NICK=ABC
1, ID=0x11 0x22,0x33,0x44
1, NICK=XYZ

There is my code:

#define SUM 2

struct record {
  byte id[4];
  char nick[8];  
} rec[SUM];

rec[0].id = { 0x10, 0x23, 0x94, 0x3A };
rec[0].nick = "ABC";

rec[1].id = { 0x11, 0x22, 0x33, 0x44 };
rec[1].nick = "XYZ";

void setup() {
  Serial.begin(9600);
  for (byte i = 0; i < SUM; i++) {
    Serial.print(i);
    Serial.print(", ID=");
    for (byte j = 0; j <  rec[i].id.length(); j++) {
      Serial.print(rec[0].id[j]);
      Serial.print(",");
    }
    Serial.print(i);
    Serial.print("\n");
    Serial.print("NICK=");
    for (byte j = 0; j < sizeof(val.nick)/sizeof(byte) ; j++) {
      Serial.print(rec[0].nick[j]);
      Serial.print(",");
    }
    Serial.print("\n");    
  }    
}

void loop() {}

But compilation fail:

struct_test.ino:8:1: error: 'rec' does not name a type
struct_test.ino:9:1: error: 'rec' does not name a type
struct_test.ino:11:1: error: 'rec' does not name a type
struct_test.ino:12:1: error: 'rec' does not name a type
struct_test.ino: In function 'void setup()':
struct_test.ino:19:37: error: request for member 'length' in 'rec[((int)i)].record::id', which is of non-class type 'byte [4] {aka unsigned char [4]}'
struct_test.ino:24:33: error: 'val' was not declared in this scope

What I do bad?

struct record
{
  byte id[4];
  char nick[8];  
};

struct record rec[SUM] =
{
      { { 0x10, 0x23, 0x94, 0x3A }, "ABC" }
    , { { 0x11, 0x22, 0x33, 0x44 }, "XYZ" }
};

You'll have other problems as well with 'rec*.id.length()'*

You can populate those arrays when you declare the struct, or you can go one at a time putting things in there. But you can't use brace initializer to set the struct up after you've already declared it.

Define your struct array like this:

struct record {
     byte id[4];
     char nick[8];
}

record REC[SUM] = {
     {  {0x10, 0x23, 0x94, 0x3A} , "ABC" },
     {  {0x11, 0x22, 0x33, 0x44} , "XYZ" }
};

That's a lot of braces. I hope I got that right.

EDIT: @lloyddean with the ninja post. Glad we got the same answer.

Thank you. It works.

But how can I set/change rec[i].id in program? For example user put ID to serial port and I need save it to rec[i].id ?

I try this code:

#define SUM 5

struct record {
  byte id[4];
  char nick[8];  
};

struct record rec[SUM] =
{
  { { 0x10, 0x23, 0x94, 0x3A }, "ABC" },
  { { 0x11, 0x22, 0x33, 0x44 }, "XYZ" }
};

void setup() {
  Serial.begin(9600);
  for (byte i = 0; i < SUM; i++) {
    Serial.print(i);
    Serial.print(", ID=");
    for (byte j = 0; j <  sizeof(rec[i].id)/sizeof(byte); j++) {
      Serial.print(rec[0].id[j]);
      Serial.print(",");
    }
    Serial.print(i);
    Serial.print("\n");
    Serial.print("NICK=");
    for (byte j = 0; j < sizeof(rec[i].nick)/sizeof(char) ; j++) {
      Serial.print(rec[0].nick[j]);
      Serial.print(",");
    }
    Serial.print("\n");    
  }    

  rec[3].id = { 0x10, 0x23, 0x94, 0x3A }; // error
  
  byte data[] = { 0x10, 0x23, 0x94, 0x3A };
  fillArrayID(*data, *rec[3].id);   // error
  rec[3].nick = "ABC";
}

void fillArrayID(byte *src[], byte *dst[]) {
  for (byte i=0; i < sizeof(src)/sizeof(byte) {
    dst[i] = src[i];
  }
}

void loop() {}

But compilation fail:

struct_test.ino: In function 'void setup()':
struct_test:35: error: invalid conversion from 'byte {aka unsigned char}' to 'byte** {aka unsigned char**}' [-fpermissive]
struct_test:5: error: initializing argument 1 of 'void fillArrayID(byte**, byte**)' [-fpermissive]
struct_test:35: error: invalid conversion from 'byte {aka unsigned char}' to 'byte** {aka unsigned char**}' [-fpermissive]
struct_test:5: error: initializing argument 2 of 'void fillArrayID(byte**, byte**)' [-fpermissive]
struct_test:36: error: incompatible types in assignment of 'const char [4]' to 'char [8]'
struct_test.ino: In function 'void fillArrayID(byte**, byte**)':
struct_test:40: error: expected ';' before '{' token
struct_test:40: error: expected primary-expression before '{' token
struct_test:40: error: expected ')' before '{' token
invalid conversion from 'byte {aka unsigned char}' to 'byte** {aka unsigned char**}' [-fpermissive]
]/code]

Yeah, like I said, you only get to use brace initializer in the initial definition. If you want to change the array later you either have to step through it one element at a time or use memcpy.

martin159:
But how can I set/change rec[i].id in program? For example user put ID to serial port and I need save it to rec[i].id ?

  rec[3].id = { 0x10, 0x23, 0x94, 0x3A }; // error

you'll have to do them one at a time, either in a loop or seperately

  rec[3].id[0] = 0x10;
  rec[3].id[1] = 0x23;
  rec[3].id[2] = 0x94;
  rec[3].id[3] = 0x3A;