atoi() for 2 dimensional array

Ruby

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

Steve Summit

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

convert 3 dimensional array to a 2 dimensional array

From Dev

Filter 2 dimensional array

From Dev

2 dimensional array PHP

From Dev

2 dimensional array of TCHAR

From Dev

Init a 3 dimensional array with 2 dimensional arrays

From Dev

Typedef for 2 dimensional array in C

From Dev

2 dimensional array in C language

From Dev

Iteration to update 2 dimensional array

From Dev

Typedef for 2 dimensional array in C

From Dev

2 dimensional array simulation/comparison

From Dev

Using method 2 dimensional array?

From Dev

Iteration to update 2 dimensional array

From Dev

Populate 2Dimensional Array

From Dev

Is there a shorthand for declaring a 2 dimensional array?

From Dev

Auto generate 2 dimensional Array,

From Dev

PHP fgetcsv 2 dimensional array

From Dev

Numpy where for 2 dimensional array

From Dev

2-Dimensional array storage

From Dev

How to improve this with a 2 dimensional array

From Dev

Sum in 2-dimensional array

From Dev

Appending to 2 dimensional array in python

From Dev

How to group array 1 dimensional to array 2 dimensional?

From Dev

Matlab: reshape 3-dimensional array into 2-dimensional array

From Dev

Numpy indexing 3-dimensional array into 2-dimensional array

From Dev

Create 3-dimensional array from 2 dimensional array in matlab

From Dev

How to flatten a 2-dimensional array into a 1-dimensional array

From Dev

Efficient way to manipulate 3 dimensional array to 2 dimensional array in MATLAB

From Dev

How to group array 1 dimensional to array 2 dimensional?

From Dev

Comparing values of 2 dimensional array to single dimensional array