const char* or const(char)* when porting C headers?

Benjamin Thaut code at benjamin-thaut.de
Sun Dec 22 12:04:27 PST 2013


Am 22.12.2013 20:34, schrieb Gary Willoughby:
> On Sunday, 22 December 2013 at 18:28:43 UTC, Benjamin Thaut wrote:
>> Am 22.12.2013 18:39, schrieb Gary Willoughby:
>>> Ah right, so:
>>>
>>> struct Tcl_Obj * CONST * objv
>>>
>>> would be:
>>>
>>> const(Tcl_Obj*)* objv  or  const(Tcl_Obj*)[] objv
>>
>> Yes
>
> Great thanks! I thought i had a pretty good handle on C but porting some
> headers makes me scratch my head.

Yes, its sometimes really astonishing in what ways C-features can be 
abused. I very recently sumbled apon this:

void (*callbackFunc)(GtkWidget* widget, void* userData);

void registerCallback(callbackFunc func);

void userCallback(GtkEntry* entry)
{
   ...
}

void someFunc()
{
   registerCallback((callbackFunc)&userCallback);
}

Note that the signature of the funciton does not match at all. The first 
parameter is "casted" automatically to a different data type, which only 
works because pointers always have the same size and the second paramter 
is omitted completely. This only works because of the C calling 
convetion. C can be a strange land ;-)

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d-learn mailing list