For example i have:
typedef struct {
uint32_t* param_ptr;
uint32_t (*check_value)(uint32_t);
} Parameter;
uint32_t a = 8;
const Parameter work = { .param_ptr = &a, .check_value = (uint32_t value) {return value>10?value:10;} };
int main1(void) {
//check
*work.param_ptr = work.check_value(*work.param_ptr);
}
I want to declare 'mini' function inside struct initialization. As there a lot of 'parameters' i don't want to declare separate functions and their body, and pass it's name to initialization. Anyway to do so?
UPD1:
#define lambda(return_type, function_body) \
({ \
return_type __fn__ function_body \
__fn__; \
})
typedef struct {
uint32_t* param_ptr;
uint32_t (*check_value)(uint32_t);
} Parameter;
uint32_t a = 8;
void main(void) {
Parameter work = { .param_ptr = &a, .check_value = lambda(uint32_t, (uint32_t value){return value > 10 ? value : 10;}) };
*work.param_ptr = work.check_value(*work.param_ptr);
}
Thanks for comments, i found familiar QA by keyword. Well, this way it works fine, but some cons - it is not global constant and thus saved in RAM, not FLASH of my mcu. (tool GNU C11)