array of structures won't take data

Some real quick background, I programmed a lot of C, but that was a long time ago and that might be a factor here. Having said that, I have looked (googled, etc) for a solution to my problem and what I find supports what I am doing, but it still doesn’t work.

What I want to do is create an array of structures. That works as expected (I think). But as soon as I try to place any data in any member I get an error message to the effect of:

…: error: expected constructor, destructor, or type conversion before ‘.’ token

It occurs with the last line of the included code. Without that line installed (commented out), the program (much more than what is shown here) compiles cleanly.

typedef struct LED {
  int         blink_state;            
  int         blink_on_timer;          
  int         blink_off_timer; 
  const int   blink_on_period;
  const int   blink_off_period; 
};

struct LED led_array[20];

led_array[0].blink_state = 1;  //<===  here's the highlighted line

I hope I’m not overlooking something rudimentary here, but this worked fine in my C days. I’m quite puzzled what the problem is here. Thanks for any and all help

Lyle

Is the typedef line the first line in your sketch? If so, try adding:

byte me;

before it.

If your question and the answer that goes with it is real, then .... no, the typedef is not the first line. There are a couple of #includes and a #define before the typedef that I didn't show in the code snippet. However, your response really smacks of being otherwise, so .... thank you not.

He was being genuine. The IDE does funny things. Can you post a whole sketch that illustrates the problem?

Strange as it may seem, the byte me suggestion was serious. The Arduino IDE doesn’t compile your code directly, it adds and rearranges some things before compiling. This can mess things up if the wrong things are first.

Try it out, and see if the same error pops up.

This compiles for me:

typedef struct LED {
  int         blink_state;            
  int         blink_on_timer;          
  int         blink_off_timer; 
  const int   blink_on_period;
  const int   blink_off_period; 
};

struct LED led_array[20];
void setup ()
  {
  led_array[0].blink_state = 1;  //<===  here's the highlighted line
  }  // end of setup
void loop ()  { }

Oh just wait till you discover Classes!

But remember on Arduino UNO there is only 2k RAM. Leo has 2.5k. Dynamic allocation, the whole make and delete objects thing either has to be very limited or very controlled. It makes more sense to pre-allocate buffer space for an array and fill that in dynamic fashion.

Stick with C string.h strings on MCU's and avoid the C++ Container Classes like String!

Nick, it does for me too when I put the value assignments where it belongs. INSIDE the setup routine. I tried to do it immediately after the struct as an array declaration. I can't say rookie mistake, I did a lot of C but that was MANY years ago. I'm just very rusty. VERY rusty.

Also my apology for my first response. The answer did seem a bit off putting without any background. I was torn as I can see PaulS has 40k+ posts which means he's not here to snipe. I'm new here so I don't know who is who, but again not an excuse.

Lyle

That’s why we like a sketch that compiles that demonstrates a problem and not excerpts. You are lucky PaulS didn’t mention his favourite web site: http://snippets_R_us.com

I’m looking at a problem in another thread which goes away when I attempt to reproduce it, which makes me think that the omitted “unimportant details” are in fact important.

The error you would have got would help you learn though.

The type and array definitions look as if they are defining global data, which implies that this code is global and not in any function, but obviously you can’t make variable assignments outside of functions. Perhaps you’ve forgotten the distinction between providing initialisation values as part of a variable definition, and making assignments to a variable which is already defined?

PeterH: The type and array definitions look as if they are defining global data, which implies that this code is global and not in any function, but obviously you can't make variable assignments outside of functions. Perhaps you've forgotten the distinction between providing initialisation values as part of a variable definition, and making assignments to a variable which is already defined?

I with embarrassment say that that is exactly what had happened. It's been a long time since I regularly programmed in C , am finding I am making rookie mistakes. The snippet as it appears is exactly how it appeared in the sketch, all in the global area. As soon as Nick showed the value assignments in the setup routine, the bulb in my head went on brightly as to what my problem was.