内存buffer转换为多维数组访问

一段内存按照多维数组或者矩阵的形式去访问,定义多维数组指针,代码看起来清晰简洁。

int32_t reshape(uint8_t *buf, uint32_t m, uint32_t n)
{
    typedef uint8_t (*dim2)[m][n];
    dim2 p = buf;
    printf("buf=%p, %p, m=%d, n=%d
", buf, p, m, n);
    printf("buf: %x, %x, %x, %x
",&buf[0], &buf[1], &buf[2], &buf[3]);
    printf("(*p)[x][y]: %x, %x, %x, %x
", (*p)[0][0], (*p)[1][0], (*p)[2][0], (*p)[3][0]);
  return 0;
}
int main()
{
    uint8_t buf[] = {1,2,3,4,5,6,7,8};
    reshape(buf, 4, 2);
    return 0;
}

原文地址:https://www.cnblogs.com/brightmind/p/13523074.html