以下c ++代码的作用是什么

Nit*_*arg 0 c++ arrays malloc

我知道它非常天真的问题,但我无法理解以下代码的作用.

#include <malloc.h>
#define MAXROW 3
#define MAXCOL 4
int main(){
int (*p)[MAXCOL];
p = (int (*)[MAXCOL])malloc(MAXROW*sizeof(*p));
}
Run Code Online (Sandbox Code Playgroud)

请提供完整的说明,包括p的类型和大小.

It is just for learning purpose. I am not using this code in any real application.
Run Code Online (Sandbox Code Playgroud)

Lun*_*din 5

据我所知,这是胡言乱语.你可能意味着(int(*)[MAXCOL]).

在C语言中,这意味着编写它的程序员不知道指针类型转换是如何工作的.

在C++中,它意味着您正在分配一个数组数组.p是一个数组指针,所以*p是一个大小为MAXCOL的数组,你可以为MAXROW分配这样的数组.结果是"受损"的2D阵列.使用这种相当模糊的语法的优势在于,您获得了一个2D数组,其中包含相邻内存中的每个单元格,这是您无法通过更常见的指针到指针动态2D数组实现的.