C ++:如何将'const char *'转换为char [英] C++: How to convert 'const char*' to char

查看:204
本文介绍了C ++:如何将'const char *'转换为char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在StackOverflow上有很多问题,但我没有找到任何帮助解决我的情况。每当我尝试这样做:

  // str =一些字符串或字符数组

//一些魔法来绕过精确的错误
stringstream convert;
convert<< str;
//捕获流的临时字符串
const string store = convert.str();
//获取一个可管理的数组
const char * temp = store.c_str();

然后尝试执行 atoi(temp [0]),我继续得到经典的转换错误,char无法转换为const char。在 atoi 和许多其他函数的文档中,const char是必需的参数。如果只有一个字符,如何发送一个字符?检索特定位置的字符是否自动转换为字符?

解决方案

我不确定这是什么原因错误,但是atoi取其参数不是一个char,而是指向一个。所以代替
atoi(temp [0])



请尝试此

 code> atoi(& temp [0])

/ p>

I know there are a lot of questions like this out there on StackOverflow, but I haven't been able to find any that help resolve my case. Whenever I try to do something like this:

// str = some string or char array

// some magic to get around fpermissive errors
stringstream convert;
convert << str;
// capture the stream's temporary string
const string store = convert.str();
// get a manageable array
const char* temp = store.c_str();

and then try to do something like atoi(temp[0]), I keep getting the classic conversion error that char couldn't be converted to const char. In the documentation for atoi and many other functions, const char is a required parameter. How can a char be sent in if there's only a const one? Does retrieving a char at a specific position auto-cast to char?

解决方案

I'm not sure if this is what is causing the error, but atoi takes as its parameter not a char, but the pointer to one. So instead of atoi(temp[0])

try this

atoi(&temp[0])

as that is a pointer.

这篇关于C ++:如何将'const char *'转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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