On Sunday 13 March 2011 21:32:49 Gene P. Cross wrote: > Hi, I'm fairly new to D and I'm writing a Space Invaders clone to get > myself acquainted with the language. > > I'm having trouble passing D strings (char[]) to SDL, in particular > SDL_LoadBMP(), I keep receiving a segfault. > > Heres the code: > > void setImg(string path) { > // concat null terminating char to string and cast to c type string > when // passing to SDL_LoadBMP() > path ~= "\0"; > image = SDL_LoadBMP( cast(char*)path ); > } > > and the value of path is "./resources/cannon.bmp" > > I'm using SDL 1.2.14 and DMD 1.067 on Ubuntu 10.10 > > If someone could tell me what I'm doing wrong it would be greatly > appreciated.
I don't use D1, so I don't know all of the ins and outs, but you're going to have to make sure that you put a "\0" at the end of the string so that it's properly null-terminated (which you appear to be doing), and you should be passing the string's ptr property to the function, not casting the string to char*. If you were dealing with D2, you'd also have to worry about not passing a string (as opposed to a char[]) to a C function if there's _any_ chance that it would alter it, since string in D2 is immutable(char)[], but that's not the case in D1, so that shouldn't be a problem with what you're doing. - Jonathan M Davis