今天看到这样一种写法,乍一看没回过神,以为前者等同后者

前者:

#define IP_PCB \
  /* ip addresses in network byte order */\
  ip_addr_t local_ip;               \
  ip_addr_t remote_ip;              \
   /* Socket options */             \
  u8_t so_options;                  \
   /* Type Of Service */            \
  u8_t tos;                         \
  /* Time To Live */                \
  u8_t ttl                          \
  /* link layer address resolution hint */ \
  IP_PCB_ADDRHINT


后者:

#define IP_PCB   ip_addr_t local_ip;               \
#define IP_PCB   ip_addr_t remote_ip;              \
#define IP_PCB   u8_t so_options;                  \
#define IP_PCB   u8_t tos;                         \
#define IP_PCB   u8_t ttl                          \
随口一说,被某人一阵损。囧。2就2吧,写个代码测试测试。
#include<stdio.h>
#include<stdlib.h>
#define TCP_PCB_COMMON(type)  type *next;  \
  void *callback_arg; unsigned char prio;  \
  int local_port 

int main()
{
	  TCP_PCB_COMMON(int) ;
           local_port  = 20;
	  return 0;
}
 
又把另外的一个#和##研究了下
 
//example one
#define WARN_IFF(EXPR) \
do{ \
  if(EXPR) \
   fprintf(stdout,"Wanring"#EXPR"\n"); \
 }while(0)  //example two
#define LINK_MULTIPLE(a,b,c,d) a##b_##b##_##c##_##d
typedef struct _record_type LINK_MULTIPLE(name,company,position,salary);
/*
 *替换成 typedef struct _record_type name_company_position_salary
 */  //example three
#define PARSER(N) printf("token"#N " = %d\n" ,token##N)
int token64 = 64;
/*
 * 将被解析为 printf("token" "64" " = %d\n" ,)
 */  //example four
struct command
{
 char *name;
 void (*function)(void);
};
#define COMMAND(NAME){NAME,NAME##_command}
void quit_command(void){}
void help_command(void){}
  //example five
//#define myprintf(templt,...) fprintf(stderr,templt,##__VA_ARGS__)
//#define myprintf(templt, ... ) fprintf(stderr,templt,__VA_ARGS__)
//#define myprintf(templt, __VAR_ARGS__... ) fprintf(stderr,templt,##__VAR_ARGS__) 
#define myprintf(templt, ... ) fprintf(stderr,templt,##__VA_ARGS__)  //默认的宏是__VA_ARGS__而不是__VAR_ARGS__
//#define myprintf(templt,args...)fprintf(stderr,templt,##args)

 int main()
{
   //TCP_PCB_COMMON(int) ;
      //local_port  = 20;    int driver = 0;
   char  *quit = NULL ;
   char  *help = NULL;
   //char  quit_command = 0;
   //char  help_command = 0;
   struct command commands[] = {COMMAND(quit),COMMAND(help)};
      
   WARN_IFF(driver == 0);
   PARSER(64);
   myprintf("ERROR\n");
   return 0;
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值