Initialising a struct of arrays

I won’t post my whole sketch here, it’s over a thousand lines, but here is the thing I’m struggling with.

The following appears to be fine:

struct serverData
{
  char settingName[30];      // for storing a name of the server setting (e.g. "Ceiling Light")
  int settingNumberVal;      // for storing a numerical value for the setting if applicable (e.g. 30)
  char settingTextVal[120];  // for storing a text value for the setting if applicable (e.g. audio track name)
  int gotSettingName;        // (e.g. if the settingName was downloaded successfully, this is set to 1)
  int gotSettingVal;         // (e.g. if a settingTextVal OR settingNumberVal was downloaded successfully, this is set to 1)
};

typedef struct serverData ServerData;

ServerData lightCircuit[6];  // store circuit names and light levels for 6 light circuits in a room

And in my setup(), this compiles (though I don’t think my settingName will work):

  for (int i=0;i<6;i++) {
    lightCircuit[i].settingName[30] = '--';       // Value not yet received from server
    lightCircuit[i].settingNumberVal = -1;        // Init this value to -1 to show it hasn't been set yet
    lightCircuit[i].gotSettingName = 0;           // We don't yet have the light name
    lightCircuit[i].gotSettingVal = 0;            // We don't yet have the light level
  }

But when I try to access the settingName, e.g.

Serial.println(lightCircuit[currentCircuit].settingName);

Predictably it doesn’t work.

(currentCircuit is an int, used for determining which circuit we are currently operating)

I say “predictably” because I pretty much made the thing compile by trial and error, rather than actually understanding why I had to initialise my text value like that. I tried this:

    lightCircuit[i].settingName = "--";       // Value not yet received from server

as I expected this to work - but it didn’t compile.

Scuse the pun, but any pointers?

strcpy(lightCircuit[i].settingName,"--");

You don't have use typedef anymore.

struct SERVERDATA_STRUCT
{
  ...
};


SERVERDATA_STRUCT lightCircuit[6];  // store circuit names and light levels for 6 light circuits in a room

You can fill a variable when declaring it.

SERVERDATA_STRUCT lightCircuit[6] = 
{
  {
    "Hello", 10, "Hello", 0, 0,
  },
  {
  },
};

But when you fill it runtime, you can assign a pointer to a string in ram, but you can not make the compiler generate code to actually fill the array in the struct. So you have to use the strcpy as marco_c wrote.

When you want to set the complete arrays of structures to predefined values, you might want to put it in PROGMEM :stuck_out_tongue: