简体   繁体   中英

C++: How to convert 'const char*' to char

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM