issue with Array (cannot convert '<brace-enclosed initializer list>' to 'uint16)

Hi guys,

I have this part of code :

  struct __attribute__((packed)) DATASRUCT {
  uint16_t  mac[6];
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;
  DataStruct.temp = 25.00;

   DataStruct.mac[6]={0x36,0x35,0x35,0x35,0x35,0x35};

but when i compile it gave me this error:

cannot convert '<brace-enclosed initializer list>' to 'uint16_t {aka short unsigned int}' in assignment

Any help please :slight_smile:

thanks for all

Please post a complete program that illustrates the error

the error in part of DataStruct.mac[6]={0x36,0x35,0x35,0x35,0x35,0x35};

which says: cannot convert ‘’ to ‘uint16_t {aka short unsigned int}’ in assignment

DataStruct.mac[6]={0x36,0x35,0x35,0x35,0x35,0x35};

That line, wherever it is in your program, is trying to set element 6 of an array with a maximum element number of 5 to a list of values. There are at least 2 mistakes there

thank you UKHeliBob :slight_smile:
But any guidance how to solve it

because I want to add an variable is mac[6] inside DataStruct :

 struct __attribute__((packed)) DATASRUCT {
  uint16_t  mac[6];
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;

and specify a value after that:

   DataStruct.mac[6]={0x36,0x35,0x35,0x35,0x35,0x35};

Is the value of the mac address going to change in the program ?

after that I am going to send it with this command:

uint8_t byteArray[sizeof(DataStruct)];
  memcpy(byteArray, &DataStruct, sizeof(DataStruct));
  esp_now_send(macAddr, byteArray, sizeof(DataStruct));

You didn't answer the question as to whether the mac address is going to change within the program

no will not change!

MKSaeed:
no will not change!

Then initialise its value in the struct declaration

struct __attribute__((packed)) DATASRUCT
{
  uint16_t  mac[] = {0x36, 0x35, 0x35, 0x35, 0x35, 0x35};
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;

UKHeliBob:
Then initialise its value in the struct declaration

struct __attribute__((packed)) DATASRUCT

{
  uint16_t  mac = {0x36, 0x35, 0x35, 0x35, 0x35, 0x35};
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;

it will change sorry I understand you mean then MAC will change after: DataStruct.mac[6]={0x36,0x35,0x35,0x35,0x35,0x35};

If the value of DataStruct.mac will change within the program then you can do something like this

struct __attribute__((packed)) DATASRUCT
{
  uint16_t  mac[6];
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;

uint16_t someMacs [][6] =
{
  {0x36, 0x35, 0x35, 0x35, 0x35, 0x35},
  {0x91, 0x92, 0x93, 0x94, 0x95, 0x96}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  memcpy(DataStruct.mac, someMacs[0], sizeof(someMacs[0]));
  printMac();
  memcpy(DataStruct.mac, someMacs[1], sizeof(someMacs[1]));
  printMac();
}

void printMac()
{
  for (int x = 0; x  < 6; x++)
  {
    Serial.print(DataStruct.mac[x], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

void loop()
{
}

UKHeliBob:
If the value of DataStruct.mac will change within the program then you can do something like this

struct __attribute__((packed)) DATASRUCT

{
  uint16_t  mac[6];
  char receivedData[32];
  char button[32];
  float temp;
  float hum;
  float temp2;
  float hum2;
  uint8_t data;
  unsigned long time;
} DataStruct;

uint16_t someMacs [6] =
{
  {0x36, 0x35, 0x35, 0x35, 0x35, 0x35},
  {0x91, 0x92, 0x93, 0x94, 0x95, 0x96}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  memcpy(DataStruct.mac, someMacs[0], sizeof(someMacs[0]));
  printMac();
  memcpy(DataStruct.mac, someMacs[1], sizeof(someMacs[1]));
  printMac();
}

void printMac()
{
  for (int x = 0; x  < 6; x++)
  {
    Serial.print(DataStruct.mac, HEX);
    Serial.print(" ");
  }
  Serial.println();
}

void loop()
{
}

Yeah…thank you UKHeliBob, it works!

How does initializing the mac address in the struct definiton prevent it from being changed later in the code?
Also, it appears you need to specify that mac is 6 elements in the struct, otherwise the compiler shows an error.

How does initializing the mac address in the struct definiton prevent it from being changed later in the code?

It doesn’t, but the OP has not been forthcoming about their actual requirements. If a default MAC address is needed then it can, of course, be defined in the struct

it appears you need to specify that mac is 6 elements in the struct, otherwise the compiler shows an error.

Yes, and I find that quite odd because defining the array in the struct would seem to provide enough information to the compiler as to how much space should be allocated for it.