Call C function - Access violation
    TheDGuy via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sun Jan  3 04:30:34 PST 2016
    
    
  
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;
}
    
    
More information about the Digitalmars-d-learn
mailing list