今天看到这样一种写法,乍一看没回过神,以为前者等同后者
前者:
#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; }