如何在C中重命名/别名为函数?

问题描述 投票:0回答:1

说我有一个正在制作的库,我想调用函数renameputs,如何保留renameputs等中的原始stdlibstdio,以及其他,还有我自己的功能是puts吗?

#include <stdio.h>

alias puts original_puts;

void
puts(char *c) {
  original_puts(c);
}

我如何完成达到此目的的事情

c function alias
1个回答
0
投票

您不能为库函数起别名,但可以使用预处理程序指令为自己的库起别名。

例如:

mylib.h:

#include <stdio.h>

void my_puts(char *c);

#define puts(...) my_puts(__VA_ARGS__)

mylib.c:

#undef puts
void my_puts(char *c)
{
    puts(c);
}

#define puts(...) my_puts(__VA_ARGS__)
© www.soinside.com 2019 - 2024. All rights reserved.