mercredi 6 septembre 2017

C++ : auto variable deduction differs range for loop vs a normal for loop for multi dimensional array

Here's the following:

int ia [3][4] =  {
                        {0, 1, 2, 3},
                        {4, 5, 6, 7},
                        {8, 9, 10, 11}
                     };

  1. Using normal for loop:

    for (int (*p)[4] = ia; p != ia + 3; ++p) {
            for (int *q = *p; q != *p + 4; ++q)
                    cout << *q << " ";
            cout << endl;
    }
    
    

as i understand name of array ia is converted to the pointer to the 1st element. Hence int (*p)[4] is good.

  1. Using range for:

    for (const auto row : arr1)
            for (auto col: row)
                    cout << col << " ";
    cout << endl;
    
    

Why row is not int (*)[4]? Instead is it int *. Thanks

Aucun commentaire:

Enregistrer un commentaire