Interfacing to C

Jimmy Cao jcao219 at gmail.com
Tue Jun 28 21:23:01 PDT 2011


On Tue, Jun 28, 2011 at 11:15 PM, Joshua Niehus <jm.niehus at gmail.com> wrote:

> 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
>


I think it was written for D1, where strings are char[].
The code can be written like this:

import std.string;

extern (C) int strcmp(const char* string1, const char* string2);

int myDfunction(string s) {
    return strcmp(toStringz(s), "foo");
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110628/28389f21/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list