Call C function - Access violation
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 3 04:48:02 PST 2016
On 03.01.2016 13:30, TheDGuy wrote:
> I get an access violation with this code:
>
> extern(C) char* write(char* text);
>
> void main(string[] args){
> char[] text = "Hello World".dup; //.dup converts string to char[]
> text ~= '\0'; //append
>
> char* result = write(text.ptr); //you need .ptr
> const(char)[] s = cstr2dstr(result);
> writeln(s);
> readln(); //keep the window open
> }
>
> auto cstr2dstr(inout(char)* cstr)
> {
> return cstr ? cstr[0 .. strlen(cstr)] : "";
> }
>
> --
>
> #include std.stdio;
>
> char* write(char* text){
> return text;
> }
>
Works for me after adding the needed imports and removing the wrong
include from the C file. Is this really the actual code you're running?
Doesn't your C compiler reject that include? gcc does.
More information about the Digitalmars-d-learn
mailing list