Having trouble populating struct array

I have a structure defined like this…

typedef struct {
float field1;
float field2;
char units[20];
} calType;

calType adcCal[4];

I want to populate the default state of the elements like this…

for (int i = 0; i < NUMSENSORS; i++) {
adcCal = {

  • MA_4,*
  • MA_20,*
  • “mA”*
  • };*
    }
    But I get error no match for ‘operator=’ (operand types are ‘calType’ and ‘’)

Please edit your post to add [ code ] tags. The forum software eats some of your code if you don’t.

Arrays and strings get some special allowances when you’re creating them. For example, you can’t say someStringCar = "test"; anywhere in your code but you can put it in the initial declaration of someStringVar.

You created the array of structs here: calType adcCal[4];

You could use the {} notation to populate all the variables right there, but that’s going to be messy and error-prone.

Your for() loop must address the struct elements individually and you must use something like strcpy() to copy characters into the string.

Stated another way; do you want to do the initialization at compile time or run time?

typedef struct {
   float field1;
   float field2;
   char units[20];
} calType;

typedef in C++ is an unnecessary holdover of C. It (basically) prevents you from subsequently using the struct keyword in every reference to that struct.

C example:

struct SomeStruct{
};

struct SomeStruct someInstance; // struct keyword is necessary here in C

In C++, all struct/union/enum/class declarations behave like they are implicitly typedef'ed.

struct SomeStruct{
};

SomeStruct someInstance;