Search code examples
ctypedefpreprocessor-directive

How to define a complex type with the #define directive?


I'm learning to build complex types. Here I defined a pointer to an array 5 of shorst using typedef:

typedef short (*mytype)[5];

I'm trying to find out how to to the same with the #define directive and if it is even feasible. I tried this, but it does not work:

#define MYTYPE (short*)[5]

It seems like this directive cannot be applied for defining something more complex than a pointer or a struct. So, what is the point here?


Solution

  • How to define a [variable of a pointer to array type] with the #define directive?

    You may just use a function macro.

    #define MYTYPE(name)  short (*name)[5]
    int main() {
        short arr[5];
        MYTYPE(a) = &arr;
        typedef MYTYPE(mytype);
    }
    

    what is the point here?

    There's is no point - preprocessor is a string replacement tool that is generally not aware of C syntax. Use a typedef to define an alias for a type.