Fun with & and *

Dwight Wilson ecf_bdw at jhunix.UUCP
Fri Jun 27 06:19:05 AEST 1986


In a previous article I wrote that one could use the macro:

     #define pointer_to(X)  X *

in order to allow declarations of the form

     pointer_to(float)  x;

There are a couple more things I would like to say about this.

First, this will work on more complicated expressions, not just
the basic types.  For instance you could declare


     pointer_to(struct treenode {
                   int                         nodelabel;
                   pointer_to(struct treenode) right;
                   pointer_to(struct treenode) left;
                })  head;

this is the same as

     struct treenode {
               int nodelabel;
               struct treenode *right;
               struct treenode *left;
             }  *head;

Let's not get into an argument over which is better, I prefer the
second form myself, but can easily understand why some people would
like the first one.


Finally, the use of this macro is not limited to declarations.
Suppose we have defined the treenode structure as above.  We could
the write:

   newnode = (pointer_to(struct treenode)) malloc (sizeof (struct treenode));

instead of

   newnode = (struct treenode *) malloc (sizeof (struct treenode));

Again, which form to use is a matter of personal taste. 

                                                          -Dwight



More information about the Comp.lang.c mailing list