Within the gameOver()
function, you're trying to assign all of the values of an array with a single assignment. This isn't legal. You would normally use memcpy()
or a loop to set the values one by one. This is often done by declaring and initialising a temporary array and copying the contents into the array you want to re-initialise.
If you do copy from a temporary array, it's best practice to make it static
and const
, to inform the compiler that you don't mean to write to it and you don't need to see it re-initialised every time you execute the code. Like so:
static const unsigned short tmp[] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 };
memcpy(address, tmp, sizeof(address));
The name of your destination variable is a bit dubious, though. Typically if something was called address
I would expect it to be a pointer. If you meant it to be a pointer, and if you don't mean to change the values that address
points to, then your assignment would be nearly legal.
unsigned short const *address;
/* ... */
static const unsigned short tmp[] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 };
address = tmp;
Making tmp[]
const (and making address
a pointer to const) allows the compiler to put the data in a read-only segment, which might mean ROM on an embedded system.
If there's some hidden complexity to your code, not shown in the question, and this stops you from making address
a pointer to const
, then things would be complicated. If you changed address
to a pointer and went ahead with modifying what it points at, this might have unintended consequences.
However, the code that is shown does not look like it needs copying, and elements need be no larger than a char
to hold all their values. The temporary array could just as easily be the working array:
void gameOver()
{
unsigned short i=0;
static const unsigned char tmp[] = { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 };
while(1)
{
PORTD &=~(1<<i);
PORTB = tmp[i];
delay_ms(5);
PORTD |=1<<i;
i%8; /* <-- this has no effect, I think you meant i=(i+1)%8; */
}
}