I am trying to program a microcontroller to be able to communicate with external flash memory chip which utilizes SPI. The operation code (opcode) followed by address bytes then data bytes has to be sent in order. Instead of defining these bytes each time for different commands I want to create a structure which holds this specific order. Also I want to change the whole array inside the struct.
I have tried to create structure which has three members like opcode, address and data.
void main (void)
{
//Defining Structure
struct Command_order {
unsigned char opcode;
unsigned char address[3];
unsigned char data[5];
};
while(1)
{
struct Command_order temp = {0x02, {0x00,0x17,0x00} , {0x01,0x02,0x03,0x04,0x05}}; //Initialization of structure
temp.address = {0x1F,0x03,0xC2}; //Trying to change only address
}
}
However this won't work, Do I get structure idea wrong or is it syntax. I am new to the concept.
You cannot assign to an array as a whole. You need to assign to each array element.
temp.address[0] = 0x1F;
temp.address[1] = 0x03;
temp.address[2] = 0xC2;