是否可以使用 va_arg 将宏作为可选参数传递给可变参数函数

Is it possible to get macro passed as optional argument to variadic function using va_arg

我有以下程序。它将宏作为可选参数传递给可变参数函数。在该函数定义中,我们是否能够在不扩展的情况下获得该宏。我使用类型 'char *' 并显示宏扩展字符串。任何获得宏的方法。

#include <stdio.h>
#include <stdarg.h>

#define CONN_DISALLOW 3201008
#define SYSLOG_CONN_DISALLOW  \
    "%d: Disallowing new connections. Reason: %s.\n", CONN_DISALLOW

void cp_syslog(int syslogid, ...);

void cp_syslog(int syslogid, ...)
{
        char *syslog_disallow;
        va_list ap;
        va_start(ap, syslogid);

        syslog_disallow = va_arg(ap, char *);

        printf("String is %s", syslog_disallow);
        printf("Macro is %s", SYSLOG_CONN_DISALLOW);

        va_end(ap);
}

int main()
{
        int id = 2;
        cp_syslog(id, SYSLOG_CONN_DISALLOW);
        return 0;
}

现在得到的输出为:

String is %d: Disallowing new connections. Reason: %s.
Macro is %d: Disallowing new connections. Reason: %s.

预计为:

String is SYSLOG_CONN_DISALLOW

我的期望是,如果将不同的宏作为可选参数传递给相同的可变函数,如何处理特定的宏。

如下:

cp_syslog(id, SYSLOG_CONN_DISALLOW);
cp_syslog(id, SYSLOG_CONN_ALLOW);
cp_syslog(id, SYSLOG_ICMP_DISALLOW);

第一种可能就是做

cp_syslog(id, "SYSLOG_CONN_DISALLOW");

执行:

pi@raspberrypi:/tmp $ ./a.out
String is SYSLOG_CONN_DISALLOWMacro is %d: Disallowing new connections. Reason: %s.
pi@raspberrypi:/tmp $ 

(第一个 printfcp_syslog(id, "SYSLOG_CONN_DISALLOW\n") 中缺少换行符)

但这是人为的,甚至 cp_syslog 的定义也是人为的,因为人们知道宏 SYSLOG_CONN_DISALLOW 在其定义中。


如果你想给 'something else' 一个宏的名称和它的 'value' 只需使用一个中间宏,如 :

#define NAME_VALUE(x) #x, x

NAME_VALUE(SYSLOG_CONN_DISALLOW)的展开是"SYSLOG_CONN_DISALLOW", "%d: Disallowing new connections. Reason: %s.\n", 3201008

那个时间 cp_syslog 不必知道它适用于 SYSLOG_CONN_DISALLOW :

#include <stdio.h>
#include <stdarg.h>

#define CONN_DISALLOW 3201008
#define SYSLOG_CONN_DISALLOW  \
    "%d: Disallowing new connections. Reason: %s.\n", CONN_DISALLOW

#define NAME_VALUE(x) #x, x /* ADDED */

void cp_syslog(int syslogid, ...);

void cp_syslog(int syslogid, ...)
{
  char *syslog_disallow;
  va_list ap;
  va_start(ap, syslogid);

  syslog_disallow = va_arg(ap, char *);

  printf("String is %s\n", syslog_disallow); /* \n ADDED */
  printf("Macro is %s", va_arg(ap, char *)); /* MODIFIED */

  va_end(ap);
}

int main()
{
        int id = 2;
        cp_syslog(id, NAME_VALUE(SYSLOG_CONN_DISALLOW)); /* MODIFIED */
        return 0;
}

编译与执行:

pi@raspberrypi:/tmp $ gcc pp.c
pi@raspberrypi:/tmp $ ./a.out
String is SYSLOG_CONN_DISALLOW
Macro is %d: Disallowing new connections. Reason: %s.
pi@raspberrypi:/tmp $