1

If I have a 2D char array:

char tempoArray[2][3]={{1,2,3}, {4,5,6}};

How can I convert each entire row to one integer value using atoi() so that

printf("val1 %d\nval2 %d", val1, val2);

Will give me:

123

456

Community
  • 1
  • 1
Ruby
  • 3
  • 4
  • 3
    You don't. I mean you don't use `atoi` to do it. Instead you use normal decimal arithmetic (like `1 * 100` to get `100`, etc.). – Some programmer dude Dec 16 '16 at 09:54
  • Or if it's just for the output, why not print the three small integers next to each other without spacing between them, then it will look correct. – Some programmer dude Dec 16 '16 at 09:55
  • 1
    `char tempoArray[2][4]={{'1','2','3',0x00}, {'4','5','6', 0x00}};` then `val1= atoi(tempoArray[0]); val2= atoi(tempoArray[1]);` – LPs Dec 16 '16 at 10:22
  • You could do this at compile-time with X macros. But the code would then look really evil. Something like `#define MAGIC(i1, i2, i3) (i1 ## i2 ## i3) #define X(i1, i2, i3) MAGIC(i1, i2, i3) printf("val1 %d\nval2 %d", VALUES ); #undef X`. Not recommended! – Lundin Dec 16 '16 at 10:33
  • 1
    @LP do you feel that `0x00` is somehow "more zero" than `0`? Since it's characters you're bulding, `'\0'` would communicate slightly more. But a plain `0` is fine. – unwind Dec 16 '16 at 10:41
  • @LPs That's a neat trick. :-) – CharlesLiuChina Dec 16 '16 at 10:54
  • @unwind Professional deformation ;) – LPs Dec 16 '16 at 10:55

2 Answers2

2

If you really insist on using atoi you must transform each digit to a char first (e.g. by adding '0'). Also you must zero terminate the string.

I recommend using arithmetic, here. Just loop over the array and use the transition: x = 10*x + array[i], initializing x to 0.

LPs
  • 16,045
  • 8
  • 30
  • 61
bitmask
  • 32,434
  • 14
  • 99
  • 159
2

When you write

char tempoArray[2][3]={{1,2,3}, {4,5,6}};

your array contains small integer values like 1, 2, and 3.

atoi operates on strings, which are arrays of characters. For example, the string

"123"

consists of the three characters '1', '2', and '3', plus a trailing null character '\0'.

But the digit characters '1', '2', and '3' do not have the values 1, 2, and 3! If you hadn't known this, I encourage you to run this little program:

#include <stdio.h>

int main()
{
    printf("character '1' has value %d, number 1 has value %d\n", '1', 1);
    printf("character '2' has value %d, number 2 has value %d\n", '2', 2);
    printf("character '3' has value %d, number 3 has value %d\n", '3', 3);
}

Since the rows of your array are not strings (they are not arrays of useful character values, and they are not null-terminated), it is not possible to simply call atoi() on them.

Since the arrays contain integer values already, the most straightforward thing to do would be to do the arithmetic yourself, like this:

int i, j;
int val;

for(i = 0; i < 2; i++) {
    val = 0;
    for(j = 0; j < 3; j++) {
        val = 10 * val + tempoArray[i][j];
    }
    printf("val: %d\n", val);
}

If you really, really wanted to call atoi, you would have to construct an actual string from each array row, like this:

char tmpstring[4];

for(i = 0; i < 2; i++) {
    for(j = 0; j < 3; j++) {
        tmpstring[j] = tempoArray[i][j] + 48;  /* +48 to convert dig to char */
    }
    tmpstring[j] = '\0';

    val = atoi(tmpstring);

    printf("val: %d\n", val);
}

As you can see, this is more work, and more confusing.

One more point. To make it clear what kind of conversion was going on, I wrote

        tmpstring[j] = tempoArray[i][j] + 48;  /* +48 to convert dig to char */

But in real code I would never write this, because that "magic number" 48 is too obscure. In real code I would always write

        tmpstring[j] = tempoArray[i][j] + '0';

By definition, the value of the character '0' is exactly the right value to add to convert the number 1 to the character '1', the number 2 to the character '2', etc.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103