【发布时间】:2012-11-19 22:36:38
【问题描述】:
可能重复:
How do I work with dynamic multi-dimensional arrays in C?
Static Matrix Casting to a pointer
我的这段代码在行抛出异常
resMat[i][j] = matp[i][j];
// 代码从这里开始
#include <iostream>
#define MOD 1000000007
#define LL long long
using namespace std;
int mat[6][64][64],mat2[2][2], mat4[4][4], mat8[8][8], mat16[16][16], mat32[32][32], mat64[64][64];
int **point[6];
int resMat[64][64];
void fooMatrix(int **matp, int size)
{
int i,j;
// find canFollow of all matrixes
for(i=0;i<size;++i)
{
for(j=0;j<size;++j)
{
// throwing exception here
resMat[i][j] = matp[i][j];
}
}
}
int main()
{
point[0] = (int **)mat2;
point[1] = (int **)mat4;
point[2] = (int **)mat8;
point[3] = (int **)mat16;
point[4] = (int **)mat32;
point[5] = (int **)mat64;
LL a,b,res;
scanf("%lld %lld",&a,&b);
fooMatrix(point[a-1],1<<a);
return 0;
}
我想在我的函数 fooMatrix 中处理不同大小的 int 矩阵,比如将它存储在 resMat 中。帮我解决这个问题。
我在 windows 中使用 DevC++(g++ 编译器)。
【问题讨论】:
-
你在哪里调用函数 fooMatrix?
-
你所有的演员都错了。删除它们。
-
如果你转储#include
并使用命名空间std;对于#include ,当编译为c而不是c++时它会做什么? -
如果您将
mat2、mat4等...作为matp传递,那么它不起作用,因为这些类型(int**和int[][])描述不同内存中的数据布局。也就是说,无论任何人告诉你什么,数组和指针都不是一回事。顺便说一句——你必须强制转换以进行分配point[2] = (int **)mat8;的事实是一个提示:你已经破坏了类型系统。 -
已有many question of the years on the passing of multidimensional array to functions。 a bunch concentrating on c 也有,如果您必须使用这样的原始数组和指针,它可能会有更有用的答案。
标签: c++ pointers exception matrix