在C中返回一个typedef

mah*_*ood 1 c typedef return

在我定义的.h文件中:

#define PAIR_TYPE(type1, type2)\
    typedef struct {     \     // added \ after edit  
      type1 first;       \     // added \ after edit  
      type2 second;      \     // added \ after edit  
    };     // added ; after edit        
#define MAKE_PAIR(val1, val2) {val1, val2}
PAIR_TYPE(char *, uint32_t) mypair;
mypair foo();
Run Code Online (Sandbox Code Playgroud)

在.c文件中,我使用它像这样:

mypair foo()
{
   mypair p;
   uint32_t bar = calculate();
   p = MAKE_PAIR("normal", target);
   return p;
}
Run Code Online (Sandbox Code Playgroud)

但是我收到此错误:

错误:'{'标记之前的预期表达式

它指出的界线是:

p = MAKE_PAIR("normal", target);
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会说'{'!!! 那条线上没有'{'.

wil*_*ser 7

'{'之后你需要更多的反斜杠

#define PAIR_TYPE(type1, type2)\
  typedef struct {\
    type1 first;\
    type2 second;\
  }
Run Code Online (Sandbox Code Playgroud)