Skip to content

Instantly share code, notes, and snippets.

@zodiac1111
Created August 22, 2014 03:52
Show Gist options
  • Save zodiac1111/c86b2136d9874dd154b4 to your computer and use it in GitHub Desktop.
Save zodiac1111/c86b2136d9874dd154b4 to your computer and use it in GitHub Desktop.
c语言关键字参数(keyword argment) 命名参数(Named parament)
/* filename kargs.c
* 来自:
* http://www.darkcoding.net/software/keyword-arguments-in-c/
* http://en.wikipedia.org/wiki/Named_parameter
*/
#include <stdio.h>
/* 使用宏定义将关键字参数转换成为一个(匿名)结构体 */
/* 可以指定初始值:使用形如.x=101 */
#define draw_circle(...) draw_circle_func( \
(struct draw_circle_args){ \
.x=101, \
.y=102, \
.r=103, \
__VA_ARGS__})
struct draw_circle_args {
int x;
int y;
int r;
};
/* 实际上的函数 */
void draw_circle_func(struct draw_circle_args args) {
printf("在(%d,%d)处绘制一个半径为 %d 的圆\n",
args.x,args.y,args.r);
}
int main(void) {
draw_circle(.x=1);
draw_circle(.x=2,.y=3);
draw_circle(.x=4,.y=5,.r=6);
draw_circle(.y=7,.x=8);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment