c - 为函数使用宏

标签 c macros

我在使用宏函数时遇到以下问题。这一直有效,直到我添加了 print_heavyhitters 函数。我在 m61.h 中有这个:

#if !M61_DISABLE
#define malloc(sz)      m61_malloc((sz), __FILE__, __LINE__)
#define free(ptr)       m61_free((ptr), __FILE__, __LINE__)
#define realloc(ptr, sz)    m61_realloc((ptr), (sz), __FILE__, __LINE__)
#define calloc(nmemb, sz)   m61_calloc((nmemb), (sz), __FILE__, __LINE__)
#define print_heavyhitters(sz)  print_heavyhitters((sz), __FILE__, __LINE__)
#endif

在 m61.c 中,除了 print_heavyhitters(sz) 之外,所有这些函数都很好。我得到“- 函数 print_heavyhitters 的宏使用错误:

- Macro usage error for macro: 
 print_heavyhitters
- Syntax error

m61.c:

#include "m61.h"
...

void *m61_malloc(size_t sz, const char *file, int line) {...}

void print_heavyhitters(size_t sz, const char *file, int line) {...}

最佳答案

您对宏及其要扩展到的函数使用相同的名称。

关于c - 为函数使用宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12711158/

相关文章:

c - C中的文件内存泄漏

macros - lisp 中的宏行为问题

c - 是否应该使用宏而不是灵活的数组成员?

c - 加载我的链接列表时出现问题

c - 是否可以在 Arduino 平台上扫描所有可用的 SSID?

检查宏参数是否为指针

macros - 宏: value is not of type LIST

c++ - 宏参数上的 Foreach 宏

c - 如何修复使用strtok后打印的乱码

c - 使用有效指针的 Memcpy 段错误