Typedef

typedef vs macro

#define dPS struct s *
typedef struct s * tPS;

The intent in both cases is to define dPS and tPS to be pointers to structure s. Which
method, if any, is preferred and why?

This is a very subtle question, and anyone who gets it right (for the right reason) is to be
congratulated or condemned ("get a life" springs to mind). The answer is the typedef is
preferred. Consider the declarations:

dPS p1,p2;
tPS p3,p4;

The first expands to:

struct s * p1, p2;

which defines p1 to be a pointer to the structure and p2 to be an actual structure, which is
probably not what you wanted. The second example correctly defines p3 and p4 to be pointers.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License