Can't assign extern(C) function pointer to D variable?

ag0aep6g anonymous at example.com
Tue Nov 22 21:35:47 UTC 2022


On 22.11.22 22:11, XavierAP wrote:
> I was surprised when it didn't compile, though I immediately found it 
> understandable...
> Already read through https://dlang.org/spec/interfaceToC.html
> and https://wiki.dlang.org/Bind_D_to_C
> 
> Is it really the case (that an extern(C) function pointer cannot be 
> assigned to a D variable)? Or is it a matter of annotating with the 
> right attributes? If so, how?

Works for me:

import core.stdc.stdio: puts;
auto p1 = &puts;
extern (C) int function(const char* s) p2 = &puts;

If you're trying to assign an `extern (C)` function pointer to an 
`extern (D)` one (the default), that cannot work. The compiler would 
emit code using D's calling convention, but the called function would 
assume C's calling convention.


More information about the Digitalmars-d-learn mailing list