Search code examples
carraysatoi

Adjusting integer array based on string in C


array should have the first 0 in each pair changed to the next number in string inputlist.

Code:

{
    int array[8][8][2]= {{{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},
                {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}};
    size_t i,j,p;
    char duck;
    char inputList[66] = "01111011001111110110010110010100001011011000101111111000110100001";

    i=0;
    j=0;

    for(p=0;p<strlen(inputList);p++){
        if(i==7){
            i=0;
        }
        if(j==7){
            j=0;
        }
        duck=inputList[p];
        array[i][j][0]=atoi(duck);
        i+=1;
        j+=1;
    }



        return 0;




}

returns errors to do with atoi. What's going on?

Error:

passing argument 1 of ‘atoi’ makes pointer from integer without a cast

I'm a little confused as to what it means by cast. I feel I've muddled it up trying to compensate.


Solution

  • It looks like you are trying to use atoi to parse single-digit numbers. However, since atoi expects a C string and takes a const char*, you cannot pass it a plain char. You need to pass it a properly terminated C string. Try this: array[i][j][0]= duck-'0';