I'm new to c development and working through a few examples for the Raspberry Pi Pico. The syntax in one example struck me as odd and thus trying to understand it in a lower-level detail.
What is the order of operations for the line *buf++ = *event_str++; within the while loop below?:
static const char *gpio_irq_str[] = {
"LEVEL_LOW", // 0x1
"LEVEL_HIGH", // 0x2
"EDGE_FALL", // 0x4
"EDGE_RISE" // 0x8
};
void gpio_event_string(char *buf, uint32_t events) {
for (uint i = 0; i < 4; i++) {
uint mask = (1 << i);
if (events & mask) {
// Copy this event string into the user string
const char *event_str = gpio_irq_str[i];
while (*event_str != '\0') {
*buf++ = *event_str++;
}
events &= ~mask;
// If more events add ", "
if (events) {
*buf++ = ',';
*buf++ = ' ';
}
}
}
*buf++ = '\0';
}
For the sake of this discussion, lets say (events & mask) == 1 when i=3 (i.e. "EDGE_RISE" case. My guess based on the end result would be:
- Get the address stored in pointer
event_str - Get the address stored in pointer
buf - Get the
const charvalue ("E") stored at the address from pointerevent_str - Assign/Copy that
const charvalue ("E") to the memory at the address in pointerbuf - Increment the address on
event_strandbuf - Repeat 1-5 until
\0is found.
It was my understanding that the *event_str syntax evaluates to the const char value stored at address event_str, so why would *event_str++ increment the address and not the value at that address. How can you know whether the ++ increments the address in the pointer or the value stored at that pointer based on this syntax? Is there a good book/online reference for this syntax of copying strings in this way?