Possible Duplicate:
What is the right way to typedef a type and the same type's pointer?
I’ve recently been using Libxml2 in my project and noticed it uses typedefs like the following:
typedef struct _xmlNode xmlNode
typedef xmlNode * xmlNodePtr
The benefit of the first typedef is obvious. However I’m not sure why you’d assign an alternative name to xmlNode *. To me it’s more explicit and readable to use xmlNode * than to use xmlNodePtr but I might be missing something.
What problems would this typedef solve and also what benefits would it bring?
My opinion is typedefing object pointers is bad and should not be done.
First it alters the grammar of the C language by hiding that the declared object is of a pointer type.
Second type qualifiers (
constandvolatile) cannot penetrate a typedef.If we take your example:
It is now impossible to declare an object so the pointee is
constusingxmlNodePtralias.means
xpisconstnot*xp.By the way in the Linux kernel coding style they also recommend not to use
typedeffor pointers: