0

I tried the following code and it is giving me error.

int main() {
    string String = "1235";
    int num = atoi(String);
    cout << num << endl;
    return 0;
}
/*
error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'
     int num = atoi(String);
                          ^
mingw32-make.exe[1]: *** [Debug/main.cpp.o] Error 1
mingw32-make.exe: *** [All] Error 2 
*/

But if I use the following code it works perfectly fine.

int main() {
    char* String = "1235";
    int num = atoi(String);
    cout << num << endl;
    return 0;
}
//prints out 1235

I know I can solve my problem using stoi() function.

int main() {
    string String = "1235";
    int num = stoi(String);
    cout << num << endl;
    return 0;
}
//prints out 1235

I can solve my problem by using a char pointer instead of string. But I just want to know why this can't be done by placing string itself into atoi(). How does atoi() work internally?

I just wanna know how does atoi() function work in C++

E_net4
  • 27,810
  • 13
  • 101
  • 139
Akash Nath
  • 63
  • 4
  • 1
    [See the documentation](https://en.cppreference.com/w/cpp/string/byte/atoi). It requires a `const char *`, and a `std::string` is not a `const char *`. Also `std::string` has a `c_str()` member function that gives you the `const char *` you're looking for. – PaulMcKenzie Feb 10 '22 at 08:41
  • 6
    Don't use `atoi()` at all. It will happily accept `atoi("my cow");` and silently return `0` with no indication of error... Use `stoi()`. See [std::stoi, std::stol, std::stoll](https://en.cppreference.com/w/cpp/string/basic_string/stol) – David C. Rankin Feb 10 '22 at 08:41
  • The direct answer to the question is (ignoring you shouldn't use `atoi()`) is `atoi(String.c_str())` passing the raw c-string from `String` to `atoi()` -- but don't do it. – David C. Rankin Feb 10 '22 at 08:44
  • Just different types. One function accepts `const char *` other `const string&`. And `string` doesn't have implicit conversion operator to `const char *` – Cy-4AH Feb 10 '22 at 08:45
  • [Why shouldn't I use atoi()?](https://stackoverflow.com/q/17710018/995714), https://blog.mozilla.org/nnethercote/2009/03/13/atol-considered-harmful/ – phuclv Feb 10 '22 at 08:55
  • "I just want to know why this can't be done by placing string itself into atoi()" I don't understand the reasoning. Why should it be possible to do *anything* by just giving a string where the function asks you for `const char *` instead? Do you have the same question about `strcpy` or `strlen`, for example? Where the error message says "cannot convert", what was unclear about that? – Karl Knechtel Feb 11 '22 at 04:09

2 Answers2

1

While std::stoi accepts std::string as input, ::atoi does not.

Note: std::string is a c++ class type, const char* is a basic data type. Although std::string does have a member function .c_str(), which can return its C-Style representation with const char* type.

Protype declarations of std::stoi in <string>:

int stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
int stoi( const std::wstring& str, std::size_t* pos = nullptr, int base = 10);

Protype declaration of ::atoi in <stdlib.h>:

int atoi (const char *__nptr);
Zongru Zhan
  • 546
  • 2
  • 8
1

Because const char* and std::string are incompatible, the implicit conversion cause error.

If you still want to use std:string:

int main() {
    string String = "1235";
    int num = atoi(String.c_str());
    cout << num << endl;
    return 0;
}

see this ref.

navylover
  • 12,383
  • 5
  • 28
  • 41