<div><div><div><div class="gmail_quote">On Tue, Jun 28, 2011 at 11:15 PM, Joshua Niehus <span dir="ltr"><<a href="mailto:jm.niehus@gmail.com">jm.niehus@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<div><br></div><div>I was trying to run the example on the Interfacing to C page (<a href="http://www.d-programming-language.org/interfaceToC.html" target="_blank">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) 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:</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> writeln(myDfunction("foo"));</div>
<div>}</div><div><br></div><div>int myDfunction(const char[] s) {</div><div> return strcmp(std.string.toStringz(s), "foo");</div><div>}</div></div><div><br></div><div>Was there a reason why the consts were left out? </div>
<div>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)</div><div><br></div><div>Thanks,</div><div>
Josh </div>
</blockquote></div><br></div></div></div><div><br></div><div>I think it was written for D1, where strings are char[].<div>The code can be written like this:<br><div><br></div></div><blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<div><div><div>import std.string;</div></div></div><div><div><div><br></div></div></div><div><div><div>extern (C) int strcmp(const char* string1, const char* string2);</div></div></div><div><div><div><br></div></div></div>
<div><div><div>int myDfunction(string s) {</div></div></div><div><div><div> return strcmp(toStringz(s), "foo");</div></div></div><div><div><div>}</div></div></div></blockquote></div>