Search code examples
creturntypedef

Return a typedef in C


In a .h file I defined:

#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();

In the .c file I used it like this:

mypair foo()
{
   mypair p;
   uint32_t bar = calculate();
   p = MAKE_PAIR("normal", target);
   return p;
}

However I get this error:

error: expected expression before ‘{’ token

The line it points is:

p = MAKE_PAIR("normal", target);

I don't know why it says '{' !!! there is no '{' at that line.


Solution

  • You need more backslashes on the lines after '{'

    #define PAIR_TYPE(type1, type2)\
      typedef struct {\
        type1 first;\
        type2 second;\
      }