when I pass a string
variable in the below code, g++ gives an error:
cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
My code is:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
string a = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
But if I change the code to :
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
char a[3] = "10";
int b = atoi(a);
cout<<b<<"\n";
return 0;
}
It works completely fine.
Please explain why string
doesn't work. Is there any difference between string a
and char a[]
?
atoi
is an older function carried over from C.
C did not have std::string
, it relied on null-terminated char arrays instead. std::string
has a c_str()
method that returns a null-terminated char*
pointer to the string data.
int b = atoi(a.c_str());
In C++11, there is an alternativestd::stoi()
function that takes a std::string
as an argument:
#include <iostream>
#include <string>
int main()
{
std::string a = "10";
int b = std::stoi(a);
std::cout << b << "\n";
return 0;
}
You need to pass a C style string.
Ie use c_str()
Change
int b = atoi(a);
to
int b = atoi(a.c_str());
PS:
This would be better - get the compiler to work out the length:
char a[] = "10";
atoi()
expects a null-terminated char*
as input. A string
cannot be passed as-is where a char*
is expected, thus the compiler error. On the other hand, a char[]
can decay into a char*
, which is why using a char[]
works.
When using a string
, call its c_str()
method when you need a null-terminated char*
pointer to its character data:
int b = atoi(a.c_str());
There is a difference between them. For each one there are different functions:
As said before, for string
there is the stoi
function:
string s("20");
cout << stoi(s) * 2; // output: 40
In the past, atoi
used to handle char*
conversion.
However, now atoi
is replaced by strtol
which gets 3 parameters:
char*
of the characters to parse to long
,char**
that returns pointer to after the parsed string, int
for the base the number should be parsed from (2, 10, 16, or whatever). char c[]="20";
char* end;
cout << strtol(c, &end, 16); // output: 32
There are whole bunch of functions like strtol
like strtof
, strtod
, or strtoll
which converts to float
, double
, long
, and long long
.
The main advantages of those new functions are mostly error-handling, and multi-base support.
The main disadvantage is that there isn't a function that converts to int
, only to long
(besides other types).
For more details, see https://stackoverflow.com/a/22866001/12893141
according to the documentation of atoi()
, the function expects a "pointer to the null-terminated byte string to be interpreted" which basically is a C-style string. std::string
is string type in C++ but it have a method c_str()
that can return a C-string which you can pass to atoi()
.
string a = "10";
int b = atoi(a.c_str());
But if you still want to pass std::string
and your compiler supports C++ 11, then you can usestoi()
string a = "10";
int b = stoi(a);
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.