Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to emulate named arguments in C

Tags:

python

c

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

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

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

Motivation: I want a more Pythonic C, where I can easily manipulate my function arguments without mixing them by mistake

like image 813
CIsForCookies Avatar asked Dec 07 '22 15:12

CIsForCookies


1 Answers

Kinda, sorta, with a compound 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 wouldn't bother. It requires you to bend the function definition to accept a pointer, and makes calling it cumbersome.

like image 90
StoryTeller - Unslander Monica Avatar answered Dec 30 '22 22:12

StoryTeller - Unslander Monica