Using Postgres connection functions
Adam D. Ruppe
destructionator at gmail.com
Sat Jan 13 04:26:06 UTC 2018
On Saturday, 13 January 2018 at 04:17:02 UTC, Joe wrote:
> It only compiled after I removed the second 'const' in the
> first and second arguments.
Yeah, D's const applies down the chain automatically, so you
don't write it twice there.
> string[] keywords = ["hostaddr", "port", "dbname"];
> string[] values = ["127.0.0.1", "5432", "testdb"];
> conn = PQconnectdbParams(cast(const char **)kws,
> cast(const char **)vals, 1);
>
> So my question is: is there an easier or better way of passing
> two arrays of C null-terminated strings to an extern(C)
> function?
If and only if the values are known at compile time, you can do:
const char** keywords = ["hostaddr".ptr, "port".ptr,
"dbname".ptr, null].ptr;
or even do it inline:
PQconnectdbParams(["hostaddr".ptr, "port".ptr, "dbname".ptr,
null].ptr, ditto_for_values, 1);
Otherwise, what you did there is decent... being a C style of
array of arrays, it will need to be coded in a C style with stuff
like malloc and toStringz to convert D to C strings too.
More information about the Digitalmars-d-learn
mailing list