Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string to integer

Tags:

c++

string

I can't do this in C++

string temp = "123";
int t = atoi(temp);

why????

like image 905
Alex Avatar asked Jun 20 '26 23:06

Alex


2 Answers

That is because atoi is expecting a raw const char* pointer. Since there is no implicit conversion from std::string to const char* you get a compiler error. Use c_str() method of std::string to get a c-style const char* for a std::string object. BTW, in C++ you can use streams to do this conversion instead of using these C-style APIs.

like image 105
Naveen Avatar answered Jun 22 '26 13:06

Naveen


atoi(temp.c_str())
like image 24
Jonathan Graehl Avatar answered Jun 22 '26 11:06

Jonathan Graehl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!