D string to C struct fixed-size array

bdh ben.h at posteo.net
Sun Jan 3 08:43:34 UTC 2021


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" the struct to D:

     enum MaxTextExtent = 2053;

     struct Image
     {
       /* other members skipped */
       char[MaxTextExtent] filename;
     }


The problem is trying to set the `filename` member since I can't 
directly use a D string.  I've seen a post which is a similar 
issue[0], but adapting the code provides an error:
     static assert: "Cannot put a string into a char[2053]"

I've also tried using `new char[MaxTextExtent]` and looping 
through the string to copy the characters over.  This does 
compile, however, when trying to write the image to disk the 
provided filename is often a "stripped" version of when you want 
(e.g. "image.jpg" becomes "g").

[0]: 
https://forum.dlang.org/thread/lshnzqbfkrhfkliapicm@forum.dlang.org


More information about the Digitalmars-d-learn mailing list