Created
October 16, 2014 02:07
-
-
Save robmccoll/5236408528c8c664c201 to your computer and use it in GitHub Desktop.
This is a preprocessor hack to emulate named parameters in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
typedef struct { | |
int len; | |
int * int_arr; | |
char * char_arr; | |
} print_arr_args; | |
#define print_array(...) print_array_impl((print_arr_args){__VA_ARGS__}) | |
void print_array_impl(print_arr_args args) { | |
if(args.char_arr) { | |
for(int i = 0; i < args.len; i++) { | |
printf("arr[%d] = %c\n", i, args.char_arr[i]); | |
} | |
} else if(args.int_arr) { | |
for(int i = 0; i < args.len; i++) { | |
printf("arr[%d] = %d\n", i, args.int_arr[i]); | |
} | |
} | |
} | |
int main(int argc, char *argv[]) { | |
print_array(.len=3, .int_arr=(int[]){1,2,3}); | |
print_array(.len=7, .char_arr="testing"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment