c-宏中args的定义返回编译警告



我有以下代码:

#include <stdio.h>
#include <string.h>
#define ARGS 
    struct kk *k, 
    struct kk1 *k1 

struct kk {
    int a;
    int b;
    int (*f)(ARGS);
};
struct kk1 {
    char a;
    char b;
};

int main(int argc, char *argv[]) {
    struct kk pp = {.a = 1};
    printf("ddmsgstsssssssr ___(%d)__n", pp.a);
    return 0;
}

当我用gcc -o test test.c构建它时,我得到了以下警告:

test.c:6:9: warning: ‘struct kk1’ declared inside parameter list [enabled by default]
  struct kk1 *k1 
         ^
test.c:12:11: note: in expansion of macro ‘ARGS’
  int (*f)(ARGS);
           ^
test.c:6:9: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
  struct kk1 *k1 
         ^
test.c:12:11: note: in expansion of macro ‘ARGS’
  int (*f)(ARGS);
           ^

如何解决此问题?

struct kk1移到struct kk之前,或者向前声明struct kk1。如果您不想定义结构,后一个更优雅。

#define ARGS 
    struct kk *k, 
    struct kk1 *k1 
struct kk1; /* this changed */
struct kk {
    int a;
    int b;
    int (*f)(ARGS);
};
struct kk1 {
    char a;
    char b;
};

上移struct kk1的定义,使其位于struct kk之前。这是必需的,因为struct kk引用struct kk1

struct kk1 {
    char a;
    char b;
};
struct kk {
    int a;
    int b;
    int (*f)(ARGS);
};

前向声明存在问题,编译时发出警告:

warning: ISO C forbids forward parameter declarations

并更改为:

#define ARGS 
    struct kk *k, 
    struct kk1 *k1
struct kk1 {
    char a;
    char b;
};
/* Now kk1 is visible inside kk */
struct kk {
    int a;
    int b;
    int (*f)(ARGS);
};

但没有必要编写这种混淆的代码

int (*f)(struct kk *, struct kk1 *);

可读性更强

最新更新