Proper C Pointer Binding

bearophile bearophileHUGS at lycos.com
Wed Mar 26 04:00:29 PDT 2014


R:

>> void main() {
>>
>>  // init screen and OpenGL setup
>>
>>  auto font = loadFont(cast (char * ) "Arial.TTF");
>>
>>  scope (exit) destroyFont(font);
>>
>>  // draw some text
>>
>>  // close OpenGL and SDL with some second delay
>> }
>
> Also I am not sure about the string casting to char * here.
> I have been told that literals are 0 terminated, but strings
> usually are not. Should I in your opinion create a wrapping
> function in D that does convert the string to a proper
> char * value? And how would I do that?

Generally try to avoid casts as much as possible in D 2.066, 
because they sometimes are wrong and the compiler is not catching 
your mistakes.

Take a look at the toStringz function in Phobos.

There is also the ".ptr" attribite of dynamic arrays.

But a D string is (immutable char)[], so its ptr is (immutable 
char)*.

Bye,
bearophile


More information about the Digitalmars-d mailing list