2

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)

5
  • 1
    Not in standard C. Commented Oct 18, 2017 at 10:58
  • C doesn't have things like function expressions (also known as lambda functions). Commented Oct 18, 2017 at 10:59
  • You should post your update as an answer. Clever use of gcc extensions, by the way. Commented Oct 18, 2017 at 11:39
  • (for deleted comment) i'm always wonder how people try to use specific solution for every tool... it is not about word saving, but about less errors for code Commented Oct 18, 2017 at 11:50
  • 1
    Overall, this sounds like a "XY problem". Commented Oct 18, 2017 at 11:59

1 Answer 1

1

To begin with, you can't even initialize a variable with static storage duration to anything but constant expressions. That won't compile, neither with or without your made-up "lambda" syntax. You'll have to place the variable declaration at local scope.

Once at local scope, you can simply type .check_value = value>10?value:10 in the initializer list.

Sign up to request clarification or add additional context in comments.

6 Comments

can i make initialization in local static scope and make a global pointer to this local one?
@VasilySukhoparov Not sure what you mean with "local static scope". The above applies for any variable with static storage duration: that is, all variables declared at file scope ("globals") and all variables declared as static. If you have a plain local variable (automatic storage duration), you can of course point to it from elsewhere, though the local variable will eventually go out of scope, and after that the pointer will point at garbage.
@VasilySukhoparov What is the actual problem you are trying to solve? Why is the supposed input to your const variable in a weird format? Why can't you correct it?
i wanted global 'const structure', where all system parameters described, smth like 'min, max, value address, check function'. const coz all constants goes to flash of mcu and dont need ram. i wanted to implement 'check function' with lambda coz some parameters have influence on each other, but i do not want to make separate function declaration and definition.
@VasilySukhoparov Then the best solution that C provides, might be to declare each such constant as a #define numeric macro. These macros can be relative to each other and can include various arithmetic - as long as they are constant expressions they will still be evaluated at compile-time. And your actual struct initialization will become straight-forward, as it just uses the macro names.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.