#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
struct threshold_t {
uint8_t upper;
uint8_t lower;
};
struct threshold_abc_t {
struct threshold_t a;
struct threshold_t b;
struct threshold_t c;
};
static void print(const struct threshold_abc_t * const thresholds) {
printf("{\"a\": {\"lower\": %u, \"upper\": %u},\n" " \"b\": {\"lower\": %u, \"upper\": %u},\n"
" \"c\": {\"lower\": %u, \"upper\": %u}}\n",
thresholds->a.lower, thresholds->a.upper,
thresholds->b.lower, thresholds->b.upper,
thresholds->c.lower, thresholds->c.upper);
}
static void set_thresholds_0(struct threshold_t * const threshold,
const uint8_t lower, const uint8_t upper) {
threshold->lower = lower;
threshold->upper = upper;
}
enum threshold {
THRESHOLD_A
= offsetof(struct threshold_abc_t
, a
), THRESHOLD_B
= offsetof(struct threshold_abc_t
, b
), THRESHOLD_C
= offsetof(struct threshold_abc_t
, c
),};
static void set_thresholds_1(struct threshold_abc_t * const thresholds,
const enum threshold selection, const uint8_t lower, const uint8_t upper) {
struct threshold_t * const threshold =
(struct threshold_t * const)(&((uint8_t * const)thresholds)[selection]);
threshold->lower = lower;
threshold->upper = upper;
}
int main() {
struct threshold_abc_t thresholds = {
.a = {
.lower = 5,
.upper = 15,
},
.b = {
.lower = 0,
.upper = 20,
},
.c = {
.lower = 0,
.upper = 30,
}
};
print(&thresholds);
set_thresholds_0(&thresholds.a, 2, 50);
print(&thresholds);
set_thresholds_1(&thresholds, THRESHOLD_C, 69, 99);
print(&thresholds);
return 0;
}
I2luY2x1ZGUgPHN0ZGludC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGRlZi5oPgoKc3RydWN0IHRocmVzaG9sZF90IHsKICB1aW50OF90IHVwcGVyOwogIHVpbnQ4X3QgbG93ZXI7Cn07CgpzdHJ1Y3QgdGhyZXNob2xkX2FiY190IHsKICBzdHJ1Y3QgdGhyZXNob2xkX3QgYTsKICBzdHJ1Y3QgdGhyZXNob2xkX3QgYjsKICBzdHJ1Y3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gY29uc3QgZW51bSB0aHJlc2hvbGQgc2VsZWN0aW9uLCBjb25zdCB1aW50OF90IGxvd2VyLCBjb25zdCB1aW50OF90IHVwcGVyKSB7CiAgc3RydWN0IHRocmVzaG9sZF90ICogY29uc3QgdGhyZXNob2xkID0KICAgIChzdHJ1Y3QgdGhyZXNob2xkX3QgKiBjb25zdCkoJigodWludDhfdCAqIGNvbnN0KXRocmVzaG9sZHMpW3NlbGVjdGlvbl0pOwogIHRocmVzaG9sZC0+bG93ZXIgPSBsb3dlcjsKICB0aHJlc2hvbGQtPnVwcGVyID0gdXBwZXI7Cn0KCmludCBtYWluKCkgewogIHN0cnVjdCB0aHJlc2hvbGRfYWJjX3QgdGhyZXNob2xkcyA9IHsKICAgIC5hID0gewogICAgICAubG93ZXIgPSA1LAogICAgICAudXBwZXIgPSAxNSwKICAgIH0sCiAgICAuYiA9IHsKICAgICAgLmxvd2VyID0gMCwKICAgICAgLnVwcGVyID0gMjAsCiAgICB9LAogICAgLmMgPSB7CiAgICAgIC5sb3dlciA9IDAsCiAgICAgIC51cHBlciA9IDMwLAogICAgfQogIH07CiAgcHJpbnQoJnRocmVzaG9sZHMpOwogIHNldF90aHJlc2hvbGRzXzAoJnRocmVzaG9sZHMuYSwgMiwgNTApOwogIHByaW50KCZ0aHJlc2hvbGRzKTsKICBzZXRfdGhyZXNob2xkc18xKCZ0aHJlc2hvbGRzLCBUSFJFU0hPTERfQywgNjksIDk5KTsKICBwcmludCgmdGhyZXNob2xkcyk7CiAgcmV0dXJuIDA7Cn0=
{"a": {"lower": 5, "upper": 15},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 0, "upper": 30}}
{"a": {"lower": 2, "upper": 50},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 0, "upper": 30}}
{"a": {"lower": 2, "upper": 50},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 69, "upper": 99}}