c++一维数组指针转二维数组指针


typedef float mat4[4][4];

void mat4_set(mat4 m)
{
    for (int y = 0; y < 4; ++y) {
        for (int x = 0; x < 4; ++x) {
            m[y][x] = y * 4 + x;
        }
    }
}

template
struct matrix_traits
{
    typedef T pointer[Y][X];
};

template
inline typename matrix_traits::pointer& matrix_ptr(T* p)
{
    return *reinterpret_cast::pointer*>(p);
}

void test()
{
    float m[16] = { 0 };

    //mat4_set(matrix_ptr(m));
    mat4_set(*reinterpret_cast<float(*)[4][4]>(m));

    for (int i = 0; i < 16; ++i) {
        printf("%f\n", m[i]);
    }

}