need braces when adding char to struct?

Hello

I get this error:
"error: array must be initialized with a brace-enclosed initializer" for ATIME tOn = {hh, mm, time};

it won't let me add the char time: 06:10 to the struct, and I can't see why. If I add the string directly then it works, but thats not what I want.

typedef struct {
  byte hour;
  byte minute;
  char time[6];
} ATIME;

ATIME alarms[4];

void setupTime() {

   byte hh=6;
   byte mm=10;
   char time[6];
   char t[6] = "06:10";  // for testing
   
   for(byte i=0; i < 4; i++) {
     strcpy(time, t, sizeof(t));
     ATIME tOn = {hh, mm, time};  // this fails
//    ATIME tOn = {hh, mm, "06:10"};  // this works
    
      Serial.println(tOn.time);
     
      mm = mm + 10;
      alarms[i] = tOn;     
   }
   
}

void setup() {
  
  delay(1000);
  Serial.begin(9600);
  
  setupTime();
  
  Serial.println("-----");
  Serial.println(alarms[0].time);

}

void loop() {

}

Can anyone help
Thanks
Karl.

You can't copy arrays like that (using 'time'), use a loop and do it element by element.

You could use something like that

typedef struct {
  byte hour;
  byte minute;
  char time[6];
  void setIt(byte hh, byte mm, char* txt) {
    hour = hh;
    minute = mm;
    strncpy(time, txt, 6);
  };
} ATIME;

ATIME alarms[4];

void setupTime() {

  byte hh = 6;
  byte mm = 10;
  char time[6];
  char t[6] = "06:10";  // for testing

  for (byte i = 0; i < 4; i++) {
    sprintf(t, "%02d:%02d", hh, mm);
    alarms[i].setIt(hh, mm, t);
    //     strcpy(time, t);
    //     ATIME tOn = {hh, mm, {time}};  // this fails
    //    ATIME tOn = {hh, mm, "06:10"};  // this works

    Serial.println(alarms[i].time);

    mm = mm + 10;
  }

}

void setup() {

  delay(1000);
  Serial.begin(9600);

  setupTime();

  Serial.println("-----");
  Serial.println(alarms[0].time);

}

void loop() {

}

Probably it would be wise to store only the integers in the alarm structure
(better for consistency and the size of an alarm shrinks from 8 to 2 byte)
and generating the string representation on demand in a supplied buffer.

Structures can have memberfunctions just like classes.

karl101:
Can anyone help

Here is some example code dealing with struct array:

struct ATIME {
  byte hour;
  byte minute;
  char time[6];
};

ATIME alarms[4]= {
  {8,20,"08:20"},
  {9,30,"09:30"},
  {19,0,"19:00"},
  {20,45,"20:45"},
};


void setup() {
  Serial.begin(9600);
  Serial.println("Original values:");
  for(byte i=0; i < 4; i++)
      Serial.println(alarms[i].time);
      
  // Now set all vakues different by using code
  for(byte i=0; i < 4; i++)
  {
     alarms[i].hour=6;
     alarms[i].minute=10;
     strcpy(alarms[i].time,"06:10");
  }
  
  Serial.println("Changed values:");
  for(byte i=0; i < 4; i++)
      Serial.println(alarms[i].time);
}

void loop() {
}

BTW: You seem to keep a lot of redundant information in your struct, this is wasting RAM and requires extra programming to keep all redundant information logically consistent!