A way to emulate named arguments in C

Is there a way to use named arguments in a C function?

Something like a function with a prototype void foo(int a, int b, int c);

and I want to call him foo(a=2, c=3, b=1);[replaced the order b & cand used their names to distinguish]

Motivation: I need more Pythonic C, where I can easily manipulate the arguments of my function without mixing them by mistake

+4
source share
2 answers

Kinda, sorta, with a composite literal and designated initializers:

typedef struct foo_args {
  int a;
  int b;
  int c;
} foo_args;

// Later

foo(&(foo_args) {
  .a = 2,
  .c = 3,
  .b = 1
});

But I honestly would not bother. This requires you to bend a function definition to accept a pointer, and calls it cumbersome.

+10
source

Named arguments are not supported in C.

All arguments must be passed in the correct order.

+3
source

Source: https://habr.com/ru/post/1692210/


All Articles