嗯,基本上就是我所需要的:
extern
(al)char *
变量代码:
import std.stdio;
import std.string;
import core.stdc.stdlib;
extern (C) int yyparse();
extern (C) extern __gshared FILE* yyin;
extern (C) extern __gshared char* yyfilename;
void main(string[] args)
{
string filename = args[1];
auto file = File(filename, "r");
yyfilename = toStringz(filename);
yyin = file.getFP();
yyparse();
}
但是,toStringz
函数会返回此错误:
main.d(15): Error: cannot implicitly convert expression (toStringz(filename)) of type immutable(char)* to char*
知道出了什么问题吗?
答案 0 :(得分:5)
您可以使用:
import std.utf;
void main()
{
string filename;
char* yyfilename;
yyfilename = toUTFz!(char*)(filename);
// yyfilename = filename.toUTFz!(char*); // or with UFCS syntax
}
答案 1 :(得分:5)
问题是yyfilename
和toStringz
在传递字符串时的返回值具有不同的const限定符。 filename
是不可变的(D string
是immutable(char)[]
的别名),但是yyfilename
没有任何const限定符,因此是可变的。
您有两种选择:
yyfilename
不会在您的计划的其他位置进行修改,则应将其声明为const(char)*
而不是char*
。filename
时创建toUTFz!(char*)(filename)
的副本:{{1}}。