Macro explanation?

Scott Tringali scott.tringali at totalviewtech.com
Sun Nov 11 17:23:28 CET 2007


Randy Kramer wrote:

> But, if anybody feels like helping, I think it would help me to understand the 
> following macro (from textDisp.c).   Maybe somebody could show me what it 
> looks like expanded?  Or provide (or point me to) an explanation.
> 
> /* Macro for getting the TextPart from a  textD */
> #define TEXT_OF_TEXTD(t) (((TextWidget)((t)->w))->text)

This takes a t, which is a pointer.  It gets the "w" member of the 
struct it pointing to, which is a generic Widget* (which is a pointer). 
  It casts that to a TextWidget (which again is a pointer - don't you 
love typedefs that obscure pointerness?).  It gets the "text" member of 
the struct it is pointing to.

This could probably be more clearly coded as a function, so I'm not sure 
why it's a macro.

Basically, it's working around the fact that "w" is untyped and casts it 
back to the correct type.  (See below.)

> Also, I have a vague understanding of the "->" operator--iiuc (google isn't a 
> whole lot of help here it doesn't want to search for a "->", and I don't know 
> the name of it).  IIUC, it is used to select an element of a struct, and is 
> an alternate to the "." operator.  Further (iiuc), the "." operator is used 
> in "normal" circumstances, while the "->" is used when a pointer is involved?

In C, the -> operator is just a cleaner way of writing 
(*pointer).struct_member.

In my experience, in C, passing around pointers is faster than passing 
around entire structs, you will probably see -> more often than .


* Anyone know why we store Widget here instead of TextWidget? It seems 
to do nothing that require extra casts.



More information about the Develop mailing list