操作系统:ubuntu10.04
前言:
在开发偏大型的项目中,基本上都要使用到##和#。
一,含义
a,# -> 将宏定义的变量转化为字符串
b,##-> 将其前后的两个宏定义中的两个变量无缝拼接在一起
二,使用实例
-
/*
-
* 测试command架构
-
*/
-
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <unistd.h>
-
-
-
struct cmd_tbl_s
-
{
-
char *name; /* Command Name */
-
int maxargs; /* maximum number of arguments */
-
int repeatable; /* autorepeat allowed? */
-
/* Implementation function */
-
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]);
-
char *usage; /* Usage message (short) */
-
char *help; /* Help message (long) */
-
};
-
typedef struct cmd_tbl_s cmd_tbl_t;
-
-
-
#define CMD_MAX_COUNT 10
-
-
-
static cmd_tbl_t cmd_set[CMD_MAX_COUNT] ;
-
static int cmd_count = 0;
-
-
-
#define SR_CMD(name,maxargs,rep,cmd,usage,help) \
-
{cmd_tbl_t __sr_cmd_##name = {#name,maxargs,rep,cmd,usage,help};\
-
cmd_set[cmd_count++] = __sr_cmd_##name;}
-
-
-
static int do_test(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
-
{
-
int index = 0;
-
-
for(index = 1; index < argc; index++)
-
{
-
if(strcmp(argv[index],"--usage") == 0)
-
{
-
printf("%s usage: %s\n",cmdtp->name,cmdtp->usage);
-
}
-
else if(strcmp(argv[index],"--help") == 0)
-
{
-
printf("%s help: %s\n",cmdtp->name,cmdtp->help);
-
}
-
else
-
{
-
printf("error argv :%d, %s\n",index,argv[index]);
-
}
-
}
-
return 0;
-
}
-
-
-
void init(void)
-
{
-
SR_CMD(test,
-
2,
-
0,
-
do_test,
-
"just test",
-
"test command arch")
-
}
-
-
-
-
int main(int argc,char *argv[])
-
{
-
int index = 0;
-
-
init();
-
-
for(index = 0; index < cmd_count; index++)
-
{
-
cmd_set[index].cmd(&cmd_set[index],0,argc,argv);
-
}
-
-
return 0;
-
}
实验结果:
阅读(140) | 评论(0) | 转发(0) |