从D中的字符串获取纯字符*? [英] Obtaining a plain char* from a string in D?

查看:62
本文介绍了从D中的字符串获取纯字符*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一次绝对的麻烦中试图弄清楚如何从D字符串(一个不可变(char)[])中获取一个普通的,可变的C字符串(一个char *),以便我可以传递字符数据转换为旧版C代码。 toStringz不起作用,因为我收到一条错误消息,说我无法将immutable(char)*类型的表达式(toStringz(this.fileName())隐式转换为char *)。我是否需要重新创建一个可变的char新数组并将字符复制过来?

I'm having an absolute hell of a time trying to figure out how to get a plain, mutable C string (a char*) from a D string (a immutable(char)[]) to that I can pass the character data to legacy C code. toStringz doesn't work, as I get an error saying that I "cannot implicitly convert expression (toStringz(this.fileName())) of type immutable(char)* to char*". Do I need to recreate a new, mutable array of char and copy the characters over?

推荐答案

如果可以更改该旧C代码的D接口 ,您确定旧C代码不会修改该字符串,您可以使其接受常量(char)* ,例如

If you can change the header of the D interface of that legacy C code, and you are sure that legacy C code will not modify the string, you could make it accept a const(char)*, e.g.

char* strncpy(char* dest, const(char)* src, size_t count);
//                        ^^^^^^^^^^^^

这篇关于从D中的字符串获取纯字符*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆