digitalmars.D.learn - Interfacing to C
- Joshua Niehus <jm.niehus gmail.com> Jun 28 2011
--90e6ba21249182a68704a6d20a2e Content-Type: text/plain; charset=ISO-8859-1 Hello, I was trying to run the example on the Interfacing to C page ( http://www.d-programming-language.org/interfaceToC.html) and ran into few issues. To get it to work "as is" i was .dup(ing) strings into new chars with defined size and passing those with .ptr. Anyway it seemed like quite a bit of work for something simple so I added const in the signatures and things worked much more smoothly: import std.stdio, std.string; extern (C) int strcmp(const char* string1, const char* string2); void main() { writeln(myDfunction("foo")); } int myDfunction(const char[] s) { return strcmp(std.string.toStringz(s), "foo"); } Was there a reason why the consts were left out? if this is a typo I'd be happy to update the web page, would be good experience for a noob like me. (never used GIT before and first time participating in a community) Thanks, Josh --90e6ba21249182a68704a6d20a2e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello,<div><br></div><div>I was trying to run the example on the Interfacin= g to C page (<a href=3D"http://www.d-programming-language.org/interfaceToC.= html">http://www.d-programming-language.org/interfaceToC.html</a>) and ran = into few issues. To get it to work "as is" i was .dup(ing) string= s into new chars with defined size and passing those with .ptr. Anyway it s= eemed like quite a bit of work for something simple so I added const in the= signatures and things worked much more smoothly:</div> <div><br></div><div><div>import std.stdio, std.string;</div><div><br></div>= <div>extern (C) int strcmp(const char* string1, const char* string2);</div>= <div><br></div><div>void main()</div><div>{</div><div>=A0 =A0 writeln(myDfu= nction("foo"));</div> <div>}</div><div><br></div><div>int myDfunction(const char[] s) {</div><div==A0 =A0 return strcmp(std.string.toStringz(s), "foo");</div><div= }</div></div><div><br></div><div>Was there a reason why the consts were le=
<div>if this is a typo I'd be happy to update the web page, would be go= od experience for a noob like me. (never used GIT before and first time par= ticipating in a community)</div><div><br></div><div>Thanks,</div><div>Josh= =A0</div> --90e6ba21249182a68704a6d20a2e--
Jun 28 2011








Joshua Niehus <jm.niehus gmail.com>