digitalmars.D.learn - Cryptic C function pointer for conversion
- data pulverizer (15/15) Dec 17 2016 I have come across a function pointer in C that I am attempting
- ketmar (20/20) Dec 17 2016 On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer
- data pulverizer (7/19) Dec 17 2016 Thanks ketmar,
- ketmar (13/15) Dec 17 2016 with my head and bare hands. well, armed with some regular
- ketmar (3/3) Dec 17 2016 p.s.: that means that i didn't really *decoded* that declaration,
- bachmeier (5/7) Dec 17 2016 It may not help you, but something I've done in the past is use
- data pulverizer (3/23) Dec 17 2016 p.s. I confirmed your interpretation on stackoverflow:
I have come across a function pointer in C that I am attempting to convert, and am not sure what the current interpretation is: ``` \\ The C Code: void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); ``` The best I can tell is that this is a function pointer that returns a function that returns void and the correct translation to D is: ``` alias void function(sqlite3_vfs*,void*, const char *zSymbol) ptr; ptr* function() xDlSym; ``` I've never seen a construction like this before so my interpretation might be wrong!
Dec 17 2016
On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer wrote: that is what it means, in D: //void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); struct sqlite3_vfs {} extern(C) { alias RetRes = void function (); alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const char *zSymbol); DeclType xDlSym; void zoo (void) {} auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return &zoo; } } void main () { xDlSym = &goo; } at least that is what i managed to decode, fed to C(++) compiler and translate to D.
Dec 17 2016
On Saturday, 17 December 2016 at 14:06:07 UTC, ketmar wrote:that is what it means, in D: //void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); struct sqlite3_vfs {} extern(C) { alias RetRes = void function (); alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const char *zSymbol); ... }Thanks ketmar, I guess that this means I got it the other way round the function pointer that is returned is the function that takes in and returns void.at least that is what i managed to decode, fed to C(++) compiler and translate to D.Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package.
Dec 17 2016
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer wrote:Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package.with my head and bare hands. well, armed with some regular expressions. did you seen some of my "port" announcements? they all done manually. it's not that hard, mostly search-and-replace. also, i did used c++ 'cause it has `auto` feature, so i pasted your declaration, and then played with c++ and -Wall until it silenced. actually, void zoo (void) {} auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return &zoo; } was taken verbatim from c++. as you can see, it even has `(void)`, which is forbidden in D (but allowed in my dmd fork ;-).
Dec 17 2016
p.s.: that means that i didn't really *decoded* that declaration, just brute-forced someting that c++ compiler happily accepts. so take it with a grain of salt. ;-)
Dec 17 2016
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer wrote:Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package.It may not help you, but something I've done in the past is use Swig to create a Common Lisp interface. It translates the C to Common Lisp. That is generally much easier for me to understand.
Dec 17 2016
On Saturday, 17 December 2016 at 14:06:07 UTC, ketmar wrote:On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer wrote: that is what it means, in D: //void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); struct sqlite3_vfs {} extern(C) { alias RetRes = void function (); alias DeclType = RetRes function (sqlite3_vfs *a,void *b, const char *zSymbol); DeclType xDlSym; void zoo (void) {} auto goo (sqlite3_vfs *a,void *b, const char *zSymbol) { return &zoo; } } void main () { xDlSym = &goo; } at least that is what i managed to decode, fed to C(++) compiler and translate to D.p.s. I confirmed your interpretation on stackoverflow: http://stackoverflow.com/questions/8722817/syntax-for-a-pointer-to-a-function-returning-a-function-pointer-in-c
Dec 17 2016