On Saturday 17 November 2007 08:11 am, Tony Balinski wrote: > Quoting Randy Kramer <[EMAIL PROTECTED]>: > > Can anybody help me understand the following statement, or more > > specifically, > > the "(textBuffer *)" part: > > > > buf = (textBuffer *)XtMalloc(sizeof(textBuffer)); > Pretty close. buf is already a pointer to a textBuffer structure, but > is not pointing to anything in particular before the assignment (I suppose). > So it's got to point at some bit of memory which holds/can hold that > structure. XtMalloc here returns a pointer to such memory, freshly allocated. > The trouble is that the XtMalloc function returns a pointer to characters > (char *) - that makes no difference to the pointer value, but the compiler > only wants to allow buf to take a value which is of type textBuffer *. > > The (type)value construct is a cast: when applied to pointers, it tells the > compiler to treat the operand - value - as a pointer of type type. In our > we interpret the returned value of XtMalloc as a pointer to a textBuffer > structure rather than one to a character buffer. The assignment to buf then > becomes an assignment of type-compatible values. (No actual machine code is > generated for this. In C, the return value of a function returning a void * > pointer can be assigned directly to a pointer to any other type as a special > case, to avoid writing these casts - as malloc does; but here XtMalloc returns > char * so the cast is necessary.) In short, you're bending C's type system, > but telling the compiler that you know what you're doing.
Tony, Thanks very much! I might have recognized something like: (textBuffer)XtMalloc(sizeof(textBuffer)) ... as a cast (probably not immediately, but eventually ;-) What I have to absorb now is that the syntax (<type> *) is a cast for a pointer (and not some new fangled multiplication ;-) thanks, Randy Kramer -- NEdit Develop mailing list - [email protected] http://www.nedit.org/mailman/listinfo/develop
