0

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
shubz
  • 123
  • 1
  • 3
  • 9

5 Answers5

12

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 alternative std::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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    You need `#include ` to use `std::string` and `std::stoi()`. – Remy Lebeau May 14 '16 at 16:38
  • [http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice](why using namespace std is bad) – Ed Heal May 14 '16 at 16:39
  • @EdHeal Yes it is, but that's outside the scope of OP's question. I took what was already there and applied the least amount of changes to answer the question. – Trevor Hickey May 14 '16 at 16:40
  • @TrevorHickey: Technically, you didn't actually answer the question that was asked (why `string` doesn't work with `atoi()`), you only provided an alternative code that works with `string`. – Remy Lebeau May 14 '16 at 16:42
  • 1
    @EdHeal: that is *the* answer, yes. But Trevor's answer does not actually explain that. – Remy Lebeau May 14 '16 at 16:47
3

You need to pass a C style string.

I.e 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";
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
2

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());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

There is a difference between them. For each one there are different functions:

String

As said before, for string there is the stoi function:

string s("20");
cout << stoi(s) * 2; // output: 40

char*

In the past, atoi used to handle char* conversion.

However, now atoi is replaced by strtol which gets 3 parameters:

  1. char* of the characters to parse to long,
  2. char** that returns pointer to after the parsed string,
  3. 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

C-H-M
  • 11
  • 2
0

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 use stoi()

string a = "10";
int b = stoi(a);
stackerjoe
  • 98
  • 6