C++ Multidimensional array in existing memory

前端 未结 4 2097
既然无缘
既然无缘 2020-12-19 20:58

(This is not a duplicate of this or this that refer to fixed sizes, the issue is not to understand how pointers are stored, but if the compiler can automate the manual funct

4条回答
  •  时光取名叫无心
    2020-12-19 21:29

    Based on this answer assuming you want an array of char you can do something like

    std::vector x(1000);
    char (&ar)[200][5] = *reinterpret_cast(x.data());
    

    Then you can use ar as a normal two-dimensional array, like

    char c = ar[2][3];
    

提交回复
热议问题