【发布时间】:2012-03-15 22:45:00
【问题描述】:
可能重复:
invalid conversion fromvoid*' tochar*' when using malloc?
我正在尝试使用指针在内存中动态分配一个矩阵,但我不断收到错误消息:
|122|错误:从 'void*' 到 'int**' 的无效转换|
|124|错误:从 'void*' 到 'int*' 的无效转换|
这是我的代码,我看不出我做错了什么...这种“void*”转换对我来说没有意义...
int i,j;
int **a;
int c = 2;
/* Form the matrix */
a = malloc((nxy+1)*sizeof(int *));
for (i=0;i<=nxy;i++)
a[i] = malloc((nxy+1)*sizeof(int));
for (i=0;i<=nxy;i++)
for (j=0;j<=nxy;j++)
a[i][j] = 0;
【问题讨论】:
-
你确定用 C 编译器而不是 C++ 编译器编译吗?
-
添加到@CharlesBailey 评论。你是在 Visual Studio 的 .cpp 文件中编译它吗?
-
我实际上在使用 Code::Blocks