STRUCT - how to work with array in sruct


hi, i'm c++ begginer. use struct of record. after need print array of struct serial that:

code: [select]
0, id=0x10,0x23,0x94,0x3a
0, nick=abc
1, id=0x11 0x22,0x33,0x44
1, nick=xyz


there code:
code: [select]
#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);
  (byte = 0; < sum; i++) {
    serial.print(i);
    serial.print(", id=");
    (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=");
    (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:
code: [select]

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


what bad?

code: [select]

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 'rec.id.length()'


Arduino Forum > Using Arduino > Programming Questions > STRUCT - how to work with array in sruct


arduino

Comments