fork download
  1. #include <inttypes.h>
  2. #include <stdio.h>
  3.  
  4. #define update_last(n, X, p) \
  5.   _Generic((X), \
  6.   float *: updatef, \
  7.   default: updatei16, \
  8.   uint16_t *: updateu16 \
  9.   )(n, X, p)
  10.  
  11. // src must have type (int16_t *)
  12. void updatei16(size_t n, int16_t a[static n], void *src) {
  13. a[n-1] = *(int16_t *)src;
  14. }
  15.  
  16. // src must have type (uint16_t *)
  17. void updateu16(size_t n, uint16_t a[static n], void *src) {
  18. a[n-1] = *(uint16_t *)src;
  19. }
  20.  
  21. // src must have type (float *)
  22. void updatef(size_t n, float a[static n], void *src) {
  23. a[n-1] = *(float *)src;
  24. }
  25.  
  26. int main(void) {
  27. float ff[1] = { 2023 }, fff = 3.1416F;
  28. uint16_t uu[1] = { 2023 }, uuu = 42;
  29. int16_t ii[1] = { 2023 }, iii = -1;
  30.  
  31. printf("%f, %"PRIu16", %"PRId16"\n", ff[0], uu[0], ii[0]);
  32.  
  33. update_last(1, ff, &fff);
  34. update_last(1, uu, &uuu);
  35. update_last(1, ii, &iii);
  36.  
  37. printf("%f, %"PRIu16", %"PRId16"\n", ff[0], uu[0], ii[0]);
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
2023.000000, 2023, 2023
3.141600, 42, -1