I'm working with microcontrollers and got following working code:
.h file
const static uint8_t state0[] = "Off";
const static uint8_t state1[] = "ROMLoad";
const static uint8_t state2[] = "RAMLoad";
const static uint8_t state3[] = "PacketSend";
const static uint8_t state4[] = "Process";
.c file
switch (coapapp_vars.state) {
case 0:
packetfunctions_reserveHeaderSize(msg, sizeof(state0) - 1);
memcpy(&msg->payload[0], &state0, sizeof(state0) - 1);
break;
case 1:
packetfunctions_reserveHeaderSize(msg, sizeof(state1) - 1);
memcpy(&msg->payload[0], &state1, sizeof(state1) - 1);
break;
case 2:
packetfunctions_reserveHeaderSize(msg, sizeof(state2) - 1);
memcpy(&msg->payload[0], &state2, sizeof(state2) - 1);
break;
case 3:
packetfunctions_reserveHeaderSize(msg, sizeof(state3) - 1);
memcpy(&msg->payload[0], &state3, sizeof(state3) - 1);
break;
case 4:
packetfunctions_reserveHeaderSize(msg, sizeof(state4) - 1);
memcpy(&msg->payload[0], &state4, sizeof(state4) - 1);
break;
}
I do want to change this code to shorten the file (to have less code uploaded) by calling memcpy after the switch. Therefor I need to use a pointer that points to the correct value from .h by using the cases to fill in the pointer. Unfortunately, I have not been able to get it to work. So I'm pretty clueless where I need to use the * or &, or not at all, to get it work. I've tried out a lot of different ways but none succeeded.
The questions then are: What type of uint8_t (uint8_t/uint8_t*/uint8_t**) do I use to initialize the variable pointing to one of the char arrays? How do I fill in that (pointer) variable (ptr = state/ptr = &state/ptr=&state[0]/...)? How do I need to use the (pointer) variable in the memcpy function(ptr/&ptr/*ptr/&(*ptr)/...)?
I am aware that I will need to use a variable that I set myself for the size.
More information if needed: This code is used in OpenWSN. I'm working with a IoT device/microcontroller. I'm using their COAP implementation to send information to a browser.
Edit: Both the reserveHeaderSize function and memcpy function will be placed after the switch. As pointed out by the correct answer, this is pretty simple but I kept overlooking a small stupid error on my part: I wasn't remove the sizeof in the third argument so I was basically doing sizeof(size). Derp
If I've understood you correctly you want to assign a pointer variable in each case
so that you can call memcpy()
afterwards. If it's so it's simple. You only need to set the size to be copied in each case too, but you have already mentioned it.
That's how it goes:
uint8_t *stateptr; // pointers can be assigned to arrays
size_t size;
switch (coapapp_vars.state) {
case 0:
packetfunctions_reserveHeaderSize(msg, sizeof(state0) - 1);
stateptr = state0;
size = sizeof state0;
break;
case 1:
packetfunctions_reserveHeaderSize(msg, sizeof(state1) - 1);
stateptr = state1;
size = sizeof state1;
break;
// other cases
}
memcpy(&msg->payload[0], stateptr, size-1);
Please note that in your code you also could have written
memcpy( &msg->payload[0], state0, sizeof state0 - 1 );
instead of ..., &state0, ...
.