有没有办法传递一个std ::字符串到接受一个char *并更改其内容的函数? [英] Is there any way to pass a std::string to a function that accepts a char* and changes its contents?

查看:108
本文介绍了有没有办法传递一个std ::字符串到接受一个char *并更改其内容的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想回到编程,特别是控制台游戏。我听说诅咒是好的,所以我找到了一个curses教程,我进入了。我使用C ++,当然我想利用std :: string,但函数 getstr()只接受char *。有什么方法可以传递一个字符串作为char *,或者我必须放弃使用C ++字符串?

I'm trying to get back into programming, specifically console games. I'd heard that curses was good for that, so I found a curses tutorial and I'm getting into that. I'm using C++, and naturally I wanted to take advantage of std::string, but functions like getstr() only accept char*. Is there any way to pass a string as a char*, or am I gonna have to forgo using C++ strings?

推荐答案

仍然可以使用C ++ std :: string ,但是你必须将内容复制到缓冲区中( std :: vector< char> code>将是好的),并使用期望可写入 char * 缓冲区的函数时将其复制回来。

You can still use C++ std::string, but you'll have to copy the contents into a buffer (std::vector<char> would be good) and copy it back when using functions that expect a writable char* buffer.

这篇关于有没有办法传递一个std ::字符串到接受一个char *并更改其内容的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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