宏定义学习

来源:互联网 发布:儿童学英语软件 编辑:程序博客网 时间:2024/06/01 11:59
## 是用于链接两个字符串# 用于把名字替换成字符串
#include <stdio.h>#include <stdlib.h>typedef void (*function)(int*);void setup_add(int* a){  *a=*a+1;}void setup_plus(int* a){  *a=*a-1;}void forward_add(int* a){  *a=*a+10;}void forward_plus(int* a){  *a=*a-10;}struct type_setup_forward{  char* type;  function setup;  function forward_cpu;};struct map_type_setup_forward{  int num;  struct type_setup_forward* functions;};#define REGISTER_LAYER(name)\handle.num=handle.num+1;\handle.functions=(struct type_setup_forward*)realloc(handle.functions, sizeof(struct map_type_setup_forward*)*handle.num);\handle.functions[handle.num-1].type=#name;\handle.functions[handle.num-1].setup=&setup_##name;\handle.functions[handle.num-1].forward_cpu=&forward_##name;int main(){  int a = 100;  struct map_type_setup_forward handle;  handle.num=0;  handle.functions=NULL;   REGISTER_LAYER(add);  REGISTER_LAYER(plus);  printf("num = %d\n", handle.num);  int i = 0;  for(i = 0; i < handle.num; ++i){    printf("%s\n", handle.functions[i].type);    handle.functions[i].setup(&a);    handle.functions[i].forward_cpu(&a);    printf("%d\n",a);  }  return 1;}
0 0
原创粉丝点击