Arrays


#1

This code compiles in C but balks at run time. line # 5
1 for(y = 0; y < 10; y++)
2 {
3 for(x = 0, x < 5; x++)
4 {
5 line[x] = array[x][y];
6 }
7 }
Does C not approve assignments from a two dim array to a one dim array???
Thanks
Warren Trammell


#2

That assignment expression is a syntactically valid expression.

What’s the RT error message saying? You are probably accessing an array element that does not exist.

Compare your code to this:

//  main.m

int main (int argc, const char * argv[])
{
    int line [5] = {0, 0, 0, 0, 0};
    int array [5][10] = {
        {1, 0, 0, 0, 0, 0, 0, 0, 1, 2},
        {0, 1, 0, 0, 0, 0, 0, 1, 0, 3},
        {0, 0, 1, 0, 0, 0, 1, 0, 0, 5},
        {0, 0, 0, 1, 0, 1, 0, 0, 0, 7},
        {0, 0, 0, 0, 1, 0, 0, 0, 0, 11},
    };
    
    for (int y = 0; y < 10; y++)
    {
        for (int x = 0; x < 5; x++)
        {
            line [x] = array [x][y];
        }
    }
    for (int x = 0; x < 5; x++)
    {
        printf ("%i", line [x]);
    }
    return 0;
}

The output should be:

235711

#3

Thanks. When I copied your file into Xcode, it worked fine. Perhaps I had a typo, non visible char, in my file.
Warren Trammell