Question about array of structs

Hi everyone,

I’m trying to create an array of structs. Is the following the correct way to do it? I have a weired feeling it’s not right. Do I have to initialize each element in the array so that the array is not just an array of pointers or something? Thanks!

union four_bytes
{
  long lval;
  float fval;
  int ival;
  byte bval;
};

typedef struct {
  four_bytes time;
  four_bytes value;
} entry;

entry all_entries[10];



void setup() {}

void loop() {
  for (int i=0;i<10;i++)
  {
    all_entries[i].time.lval=i*1000;
    all_entries[i].value.fval=i*1.5;
// Do something.
  }
    
}

In general...

1. For any variable, struct, array, etc., that is declared outside of any function, the item is automatically initialized with zero value(s).

2. For variables, structs, array, etc. initialized inside a function, they are not automatically initialized unless they are declared static Static variables are initialized with zero values. Non-static variables can be initialized when they are declared by giving an initializer list (a comma-separated list of values enclosed in {} braces). If you give an initializer list and if the number of items in the initializer list is smaller than the number of elements in an array, the remaining elements of the array are initialized with zero values. If you do not give an initializer list for non-static variables, the contents are undefined. (Can be anything the compiler/linker/loader wants to put there, or even could be whatever was in that memory location from some previous use. Undefined is undefined.)

Regards,

Dave

Thanks Dave. The reason that I posted this is to get some comments before I put this part in a project that I have 1,600 lines of code already. Just making sure I'm not doing anything stupid. If this doesn't allocate the space then I would be accessing that will be allocated to other variables and mess up the SRAM.

liudr,

I haven't checked this with a compiler, but I think that since you didn't typedef the union, your struct would have to be like this:

typedef struct {
  union four_bytes time;
  union four_bytes value;
} entry;

Other than that, everything looks good to me.

Regards,

-Mike

@Mike Murdock

I haven't checked this with a compiler

Well, I have. There is nothing wrong with liudr's code. See Footnote.

The sketch gets turned into a C++ file. Unlike C, in C++ a union specification defines a type without requiring the typedef. I mean, your syntax is not incorrect, but I don't see where it adds value.

Regards,

Dave

Footnote: The original works just fine. However, since it is C++, I would probably use the following for the struct definition instead of the somewhat less elegant typedef:

union four_bytes
{
    long  lval;
    float fval;
    int   ival;
    byte  bval;
};

struct entry
{
    four_bytes time;
    four_bytes value;
};

entry all_entries[10];

And, yes, I have tested it.

Dave,

Thanks for the info. I did not realize that C++ treated struct and union definitions as typedefs.

Regards,

-Mike

Thank you Dave and Mike. My code seems stable at least after some testing now. I'll keep testing it. Almost 1,700 lines of code not. Reaching my personal limit of 2,000.