0

I have the array of the arrays of char. I need to convert it into an array of int. How can I do that? I know we can convert char* to int as follows:

char *i = "123";
std::stringstream ss;
ss << i;
int ii;
ss >> ii;
std::cout << ii << std::endl; //prints 123

But how can I convert the array of such an arrays into an array of integers?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

2 Answers2

2

You can try the atoi() function already present.

It will convert the string into a integer that you can use.

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
1
char* strings[4] = { "123", "456", "789", "555" };
int vals[4];
for (int i = 0; i < 4; ++i)
{
    std::stringstream ss;
    ss << strings[i];
    ss >> vals[i];
}
for (int i = 0; i < 4; ++i)
{
    std::cout << vals[i] << std::endl;
}
tsandy
  • 911
  • 4
  • 10