user-0 提问: #define的用法求教？

回复:
user: user-1
content: 引用 4 楼 guiguiyiha 的回复: #define dump(resrc, level, fmt, args, ...) \     do{\         struct test_struct *resource = (struct test_struct *)resrc;\         printf("resource: %p, %d\n", resource, resource->a);\         \         if (level) {\              printf("(func:%s line:%4d): \n", __func__, __LINE__, fmt, #args);\         }\     }while(0)  struct test_struct {     unsigned int a;     unsigned int b;     unsigned int c;     unsigned int d; };  int main() {     struct test_struct resource;     resource.a = 10;     resource.b = 20;     resource.c = 30;     resource.d = 40;          printf("resource: %p, %d\n", &resource, resource.a);      dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d);      } 打印结果为： resource: 0xbfd69000, 10 resource: 0xbfd68ffc, -1076457476 (func:main line:  39):  我这执行没什么问题http://ideone.com/IR6Foq

user: user-0
content: 引用 9 楼 lming_08 的回复:printf("(func:%s line:%4d): \n", __func__, __LINE__, fmt, #args); 你这个能对应起来吗？分成两个printf试试 我现在就是用2个printf实现的。

user: user-1
content: printf("(func:%s line:%4d): \n", __func__, __LINE__, fmt, #args); 你这个能对应起来吗？分成两个printf试试

user: user-0
content: 引用 6 楼 angel_su 的回复:宏是文字置换，resource有重名问题，改一下...  struct test_struct *presource = printf("resource: %p, %d\n", presource， presource->a); #define dump(resrc, level, fmt, args...) \     do{\         printf("2.0 resource: %p\n", resrc);\         struct test_struct *Presource = (struct test_struct *)resrc ;\         printf("3.0 resource: %p, %d\n", Presource, Presource->a);\         \         if (level) {\              printf("4.0 (func:%s line:%4d): \n", __func__, __LINE__, fmt, #args);\         }\     }while(0)    int main() {     struct test_struct resource;     resource.a = 10;     resource.b = 20;     resource.c = 30;     resource.d = 40;          printf("1.0 resource: %p, %d\n", &resource, resource.a);      dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d); } 标红的部分打印不出来，为什么，fmt和args与#的配合不是很明白，求解。谢谢

user: user-2
content: 宏定义变参。。。第一次见这么用！好使吗？ 好像ls火眼金睛，已经发现那个逗号问题了。此外审核以下每个'\'后有没有空格字符，这是宏常见的错误

user: user-3
content: 宏是文字置换，resource有重名问题，改一下...  struct test_struct *presource = printf("resource: %p, %d\n", presource， presource->a);

user: user-0
content: dump前的地址和传入给define中的地址，一直相差4，为什么呢？

user: user-0
content: #define dump(resrc, level, fmt, args, ...) \     do{\         struct test_struct *resource = (struct test_struct *)resrc;\         printf("resource: %p, %d\n", resource, resource->a);\         \         if (level) {\              printf("(func:%s line:%4d): \n", __func__, __LINE__, fmt, #args);\         }\     }while(0)  struct test_struct {     unsigned int a;     unsigned int b;     unsigned int c;     unsigned int d; };  int main() {     struct test_struct resource;     resource.a = 10;     resource.b = 20;     resource.c = 30;     resource.d = 40;          printf("resource: %p, %d\n", &resource, resource.a);      dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d);      } 打印结果为： resource: 0xbfd69000, 10 resource: 0xbfd68ffc, -1076457476 (func:main line:  39):

user: user-0
content: 引用 1 楼 user-5 的回复:有两处错误。 1. 在printf("resource: %p, %d\n", resource， resource->a);\中第二个逗号是中文字符！！ 2. 在dump(resource, TRACE_DEBUG, "resource: %d\n", resource.d);中dump函数里，第一个参数是指针。所以应该是dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d); 恩，改正了，搜狗输入法最近老卡，换了个qq输入法，切换起来使用不熟。 我编译通过了但是运行结果不是我想要的.运行结果是： resource: 0xbfbf0fbc, -1077997636 (func:main line:  37):

user: user-4
content: 现在的编译器一般都能定位了吧

user: user-5
content: 有两处错误。 1. 在printf("resource: %p, %d\n", resource， resource->a);\中第二个逗号是中文字符！！ 2. 在dump(resource, TRACE_DEBUG, "resource: %d\n", resource.d);中dump函数里，第一个参数是指针。所以应该是dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d);

user: user-0
content: 引用 11 楼 lming_08 的回复:[quote=引用 4 楼 guiguiyiha 的回复:]  #define dump(resrc, level, fmt, args, ...) \     do{\         struct test_struct *resource = (struct test_struct *)resrc;\         printf("resource: %p, %d\n", resource, resource->a);\         \         if (level) {\              printf("(func:%s line:%4d): \n", __func__, __LINE__, fmt, #args);\         }\     }while(0)  struct test_struct {     unsigned int a;     unsigned int b;     unsigned int c;     unsigned int d; };  int main() {     struct test_struct resource;     resource.a = 10;     resource.b = 20;     resource.c = 30;     resource.d = 40;          printf("resource: %p, %d\n", &resource, resource.a);      dump(&resource, TRACE_DEBUG, "resource: %d\n", resource.d);      } 打印结果为： resource: 0xbfd69000, 10 resource: 0xbfd68ffc, -1076457476 (func:main line:  39):  我这执行没什么问题http://ideone.com/IR6Foq[/quote]  那你试试8楼的