Re: D string to C struct fixed-size array

2021-01-04 Thread bdh via Digitalmars-d-learn
On Monday, 4 January 2021 at 18:05:40 UTC, tsbockman wrote: /* XXX: haven't done private members yet, wonder if they're needed to * complete this */ Leaving out the private members of a struct changes its size, and sometimes its alignment. This will definitely break things if you ever

Re: D string to C struct fixed-size array

2021-01-04 Thread bdh via Digitalmars-d-learn
On Monday, 4 January 2021 at 17:24:27 UTC, Steven Schveighoffer wrote: On 1/3/21 9:17 PM, bdh wrote: On Sunday, 3 January 2021 at 11:16:25 UTC, rikki cattermole wrote: Your definition of Image is probably wrong. You may have missed a pointer (8 bytes). [...] How to test: in D-land:

Re: D string to C struct fixed-size array

2021-01-04 Thread bdh via Digitalmars-d-learn
On Monday, 4 January 2021 at 16:35:23 UTC, Jack wrote: Do you mean fill .filename member from a D string? something like this? [...] // since .filename isn't a pointer but an array, I think // you have to use memcpy() here. = operator wouldn't work properly. memcpy([0], [0],

Re: D string to C struct fixed-size array

2021-01-04 Thread tsbockman via Digitalmars-d-learn
On Monday, 4 January 2021 at 02:17:33 UTC, bdh wrote: I'm pretty sure it's correct? Here's the D version: https://repo.or.cz/magickd.git/blob/e5d06e939:/source/magickd/core/c/image.d#l751 Here's the C version:

Re: D string to C struct fixed-size array

2021-01-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/3/21 9:17 PM, bdh wrote: On Sunday, 3 January 2021 at 11:16:25 UTC, rikki cattermole wrote: Your definition of Image is probably wrong. You may have missed a pointer (8 bytes). I'm pretty sure it's correct? Here's the D version:

Re: D string to C struct fixed-size array

2021-01-04 Thread Jack via Digitalmars-d-learn
On Sunday, 3 January 2021 at 08:43:34 UTC, bdh wrote: Hi, I'm trying to create bindings to the GraphcicsMagick C library which has the following struct defined: [...] Do you mean fill .filename member from a D string? something like this? import std.stdio; [...] struct Image

Re: D string to C struct fixed-size array

2021-01-03 Thread bdh via Digitalmars-d-learn
On Sunday, 3 January 2021 at 11:16:25 UTC, rikki cattermole wrote: Your definition of Image is probably wrong. You may have missed a pointer (8 bytes). I'm pretty sure it's correct? Here's the D version: https://repo.or.cz/magickd.git/blob/e5d06e939:/source/magickd/core/c/image.d#l751

Re: D string to C struct fixed-size array

2021-01-03 Thread rikki cattermole via Digitalmars-d-learn
Your definition of Image is probably wrong. You may have missed a pointer (8 bytes).

Re: D string to C struct fixed-size array

2021-01-03 Thread bdh via Digitalmars-d-learn
On Sunday, 3 January 2021 at 09:28:55 UTC, rikki cattermole wrote: import std; void main() { int[] a = [1, 2, 3, 4, 5]; int[3] b; b[0 .. 3] = a[1 .. 4]; b.writeln; } Same principle, just remember to null terminate after slicing your dynamic array and assigning it to your

Re: D string to C struct fixed-size array

2021-01-03 Thread rikki cattermole via Digitalmars-d-learn
import std; void main() { int[] a = [1, 2, 3, 4, 5]; int[3] b; b[0 .. 3] = a[1 .. 4]; b.writeln; } Same principle, just remember to null terminate after slicing your dynamic array and assigning it to your static array.

D string to C struct fixed-size array

2021-01-03 Thread bdh via Digitalmars-d-learn
Hi, I'm trying to create bindings to the GraphcicsMagick C library which has the following struct defined: #define MaxTextExtent 2053 typedef struct _Image { /* other members skipped */ char filename[MaxTextExtent]; } Image; In an `extern (C)` block I've "converted"