我想创建一个如下所示的2D数组.
char **dog = new char[480][640];
Run Code Online (Sandbox Code Playgroud)
但它错误:
error C2440: 'initializing' : cannot convert from 'char (*)[640]' to 'char ** '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Run Code Online (Sandbox Code Playgroud)
使用"新"我需要做什么?(不使用calloc,malloc或char dog[480][640];)
像这样的东西:
char **dog = new char *[480];
for (int i = 0; i < 480; i++)
dog[i] = new char[640];
Run Code Online (Sandbox Code Playgroud)
删除时也一样,但先循环.
如果你想从堆中获取内存,可以这样使用:
// declaration
char *dog = new char[640*480];
// usage
dog[first_index * 640 + second_index] = 'a';
// deletion
delete[] dog;
Run Code Online (Sandbox Code Playgroud)