Help passing D strings to C libs

Jonathan M Davis jmdavisProg at gmx.com
Sun Mar 13 23:30:48 PDT 2011


On Sunday 13 March 2011 22:38:49 Gene P. Cross wrote:
> I've amended the source to pass the strings pointer (path.ptr) after adding
> a null but the problem still persists.
> 
> I lost for what it could be and I'm certain this is where the problem is,
> because if I remove the method call, the program runs fine.
> 
> I've noticed that calling SDL_LoadBMP and passing a string literal seems to
> work, instead of a string variable. I might load all the images
> independently into an array and have each object reference that array,
> instead of every object loading its own.

In D2 (and I assume D1, but I don't know), string literals all have "\0" one 
past their end, so you can safely pass them to C functions. Other strings don't 
have them, so you have to append the "\0". Other than that, I don't know why 
there would be any difference between passing a string literal and a regular 
string. Perhaps something else in your code is altering the string later (which 
it wouldn't do if you just passed a literal) and _that_ is screwing it up. I 
don't know. For the most part, there shouldn't be much difference between dealing 
with string literals and dealing with normal strings.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list