我的导师展示了这个 slide对于“矩阵复制”:
#define ROWSIZ 17
#define COLSIZ 27
int enamatrisen[ROWSIZ][COLSIZ];
int andramatrisen[ROWSIZ][COLSIZ];
void matcpy (int* dst, int* src)
{
int i, j;
for (i=0; i<ROWSIZ, i=i+1) /* rad-nr */
for (j=0; j<COLSIZ, j=j+1) /* kolumn-nr */
dst[i][j] = src[i][j];
}
但是
1) 说的地方有bug,
应该是;
和
2) 代码无法编译。 gcc 提示指针被用作数组或类似的东西。什么是正确的代码?这项工作进展如何?不应该为此使用 memcpy 还是这是为了实现类似 memcpy 的努力?
最佳答案
函数参数定义错误。
有关传递多维数组的更多信息: http://www.eskimo.com/~scs/cclass/int/sx9a.html
此外,for
循环中的逗号应该是分号。
void matcpy(int dst[][COLSIZ], int src[][COLSIZ])
{
int i, j;
for (i = 0; i < ROWSIZ; i = i + 1) /* rad-nr */
for (j = 0; j < COLSIZ; j = j + 1) /* kolumn-nr */
dst[i][j] = src[i][j];
}
或
void matcpy(int (*dst)[COLSIZ], int (*src)[COLSIZ])
{
int i, j;
for (i = 0; i < ROWSIZ; i = i + 1) /* rad-nr */
for (j = 0; j < COLSIZ; j = j + 1) /* kolumn-nr */
dst[i][j] = src[i][j];
}
关于c - 这个代码是不是不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12813135/