Skip to main content
Clarified "assignment" to "initialization values".
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Assigning a value to an int or char array works completely fine but this doesn't.

It only works fine if you do it on the declaration.

eg.

This works:

int foo = 42;  < ---- declare and define with initial value

This doesn't:

int foo;   // < ---- declare and define

foo = 42;  // < ---- assignment statement

Your code was like the second example.


Majenko has shown one way. He split the declaration into two parts. The first part declared the type trap_game_detail, and the second part is an instance of that type, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
};

struct trap_game_details trap_game_details[2] = {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
};

Alternatively, and a bit closer to what you were doing, just keep going with the assignmentinitialization values after you define the structure, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
} trap_game_details[2] =  
  {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
  }; 

Assigning a value to an int or char array works completely fine but this doesn't.

It only works fine if you do it on the declaration.

eg.

This works:

int foo = 42;  < ---- declare and define with initial value

This doesn't:

int foo;   // < ---- declare and define

foo = 42;  // < ---- assignment statement

Your code was like the second example.


Majenko has shown one way. He split the declaration into two parts. The first part declared the type trap_game_detail, and the second part is an instance of that type, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
};

struct trap_game_details trap_game_details[2] = {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
};

Alternatively, and a bit closer to what you were doing, just keep going with the assignment after you define the structure, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
} trap_game_details[2] =  
  {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
  }; 

Assigning a value to an int or char array works completely fine but this doesn't.

It only works fine if you do it on the declaration.

eg.

This works:

int foo = 42;  < ---- declare and define with initial value

This doesn't:

int foo;   // < ---- declare and define

foo = 42;  // < ---- assignment statement

Your code was like the second example.


Majenko has shown one way. He split the declaration into two parts. The first part declared the type trap_game_detail, and the second part is an instance of that type, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
};

struct trap_game_details trap_game_details[2] = {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
};

Alternatively, and a bit closer to what you were doing, just keep going with the initialization values after you define the structure, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
} trap_game_details[2] =  
  {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
  }; 
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Assigning a value to an int or char array works completely fine but this doesn't.

It only works fine if you do it on the declaration.

eg.

This works:

int foo = 42;  < ---- declare and define with initial value

This doesn't:

int foo;   // < ---- declare and define

foo = 42;  // < ---- assignment statement

Your code was like the second example.


Majenko has shown one way. He split the declaration into two parts. The first part declared the type trap_game_detail, and the second part is an instance of that type, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
};

struct trap_game_details trap_game_details[2] = {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
};

Alternatively, and a bit closer to what you were doing, just keep going with the assignment after you define the structure, like this:

struct trap_game_detail {
    byte flush_quantity;
    byte flush_interval;
    byte random_min;
    byte random_max;
    byte random_quantity;
} trap_game_details[2] =  
  {
    { 10, 5, 1, 100, 4 },
    { 8, 23, 5, 95, 8 }
  };