#include<iostream>
using namespace std;


int main() {
    int ia[3][4] = {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11}
    };
    for (auto p= ia;p != end(ia);++p) {  //p被初始化为指针,指向ia的首个数组
                                            //p不等于ia的尾后,自增p
        for (auto q = *p;q != end(*p); ++q) //指针q指向p当前所在行的第一个元素,*p是一个含有4个整数的数组
                                            //数组名直接使用就是被自动转换成首元素的指针
            cout << *q << endl;
    }

    return 0;
}
#include<iostream>
using namespace std;


int main() {
    int ia[3][4] = {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11}
    };
    for (int (*p)[4]= ia;p != end(ia);++p) {  //p指向含有4个整数的数组,(p代替ia为指针,指向ia下面含有4个整数的数组)

        for (int *q = *p;q != end(*p); ++q) //*q是一个int型指针
                                            //数组名直接使用就是被自动转换成首元素的指针
            cout << *q << endl;
    }

    return 0;
}
#include<iostream>
using namespace std;


int main() {
    const size_t row = 3, col = 4;
    int ia[row][col] = {
        {0,1,2,3},
        {4,5,6,7},
        {8,9,10,11}
    };
    for (auto &p:ia) {
        for (auto& p2 : p)
            cout << p2 << endl;

    }

    return 0;
}
//版本2:下标操作
    for (size_t i = 0; i != 3; ++i)
    {
        for (size_t j = 0; j != 4; ++j)
            cout << ia[i][j] << endl;
    }
    cout << endl;
    //版本3:指针操作
    for (int(*i)[4]=ia; i != end(ia); ++i)
    {
        for (int *j = *i; j != end(*i); ++j)
            cout << *j << endl;
    }
//类型别名
    using int_array = int[4]; //方式1 using xxx =
    typedef int other; //方式2 typedef int xxx
    for (int_array *p = ia; p != ia + 3; ++p)
    {
        for (other *q = *p; q != *p + 4; q++)
        {
            cout << *q << endl;
        }
    }

最后修改:2022 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏