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[]
?