array of structure inside structure - and initialisation

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Been staring at this for too long - CVAVR

typedef struct {
 uint8_t preamble[3];
 uint16_t x;
 uint16_t y;
 uint8_t tens;
 uint8_t units
 uint8_t dp;
 uint8_t tenths;
 uint8_t null;
} struct_data_time_element;

typedef struct {
 uint8_t DC1;
 uint8_t preamble[9];
 struct_data_time_element timeElement[10];
} struct_data_time_command;

#define _DC1                        0x11
#define _ESC                        0x1B
#define _F_ARIAL20                  0x0B
#define BLACK                       0x01
#define RED                         0x03
#define FONT_BACKGROUND             BLACK
#define DATA_BYTE                   0x00

struct_data_time_command data_time_command = {
_DC1, 
_ESC, 'Z', 'F', _F_ARIAL20, _ESC, 'F', 'Z', RED, FONT_BACKGROUND,
_ESC, 'Z', 'C',  90, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 130, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 170, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 210, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 250, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 290, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 330, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 370, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 410, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0,
_ESC, 'Z', 'C', 450, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE, 0};

2 problems, with compiler saying :

- constant out of range for all initialisation lines where x > 250 (can be fixed by changing 290, 330, ... all to 250, but that's not going to cut the mustard!)

- missing '}' on last line.

Any ideas? I've probably got a basic misunderstanding here somewhere :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. For every struct or array "level" you need a set of braces.
2. You had one element too many in every initializer of struct_data_time_command
3. One field in you typedef of struct_data_time_command is missing a semicolon

The initializer should look something like this:

struct_data_time_command data_time_command = 
{
   // DC1
   _DC1,
   // preamble array
   {_ESC, 'Z', 'F', _F_ARIAL20, _ESC, 'F', 'Z', RED, FONT_BACKGROUND,},
   // time_element[0]
   { {_ESC, 'Z', 'C'},  90, 90, DATA_BYTE, DATA_BYTE, '.', DATA_BYTE, DATA_BYTE},
   // Another nine struct_data_time_element initializers...
}; 

Your initial mistake was probably to try to set the whole thing up at once. Doing it step by step would have helped you immensely:
1) Create a struct_data_time_command type, with only the DC1 field, create an instance and get the initialization correct.
2) Add the preamble array to the type, and fix the initialization,
3) Create a single struct_data_time_element and get the initializer for that correct,
4) Change it to an array of struct_data_time_element and get the initializer correct,
5) Add such an array to the struct_data_time_command type and fix up the initializer.

By being impatient, and trying to take one giant leap, you collapsed. Had you taken several small steps you would have
i) disovered what went wrong when it went wrong, and
ii) had something working by now.

Happy new year!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
1. For every struct or array "level" you need a set of braces.
2. You had one element too many in every initializer of struct_data_time_command
3. One field in you typedef of struct_data_time_command is missing a semicolon
1. It seems not (see 2.)
2. yep - that was the underlying problem :embarrassed: Now that's removed it all works fine, and without embedded braces. I've no idea why I didn't see that :(
3. Well spotted - a cut'n'paste error from the .h file

I was just treating it as an "array of bytes" for initialisation purposes. If I'd not got an extra byte in every array initialisation for the internal structure it would have worked first time ;)

Happy New Year!