1
jedyu 2014-02-14 13:47:06 +08:00
定义过后,不能这么赋值
|
3
jedyu 2014-02-14 13:50:13 +08:00
int mat[3][4] = { 1, 2, 0, 4,
4, 0, 8, 10, 3, 8, 0, 12 }; |
4
yhf OP @jedyu 可是改成这样赋值以后错误是这样的:
note: candidate function not viable: no known conversion from 'int [3][4]' to 'int **' for 1st argument void setZero(int **mat, int m, int n) { ^ 1 error generated. |
5
jedyu 2014-02-14 14:21:03 +08:00
#include <iostream>
using namespace std; void setZero(int mat[3][4], int m, int n) { if (NULL == mat || 0 ==m || n == 0) { return; } bool row[m], col[n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (mat[i][j] == 0) { row[i] = true; col[j] = true; } } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (row[i] || col[j]) { mat[i][j] = 0; } } } } int main(int argc, char const *argv[]) { int mat[3][4] = { 1, 2, 0, 4, 4, 0, 8, 10, 3, 8, 0, 12 }; setZero(mat, 3, 4); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { cout << mat[i][j] << ' '; } cout << endl; } return 0; } |
6
yhf OP @jedyu 你这样编译是没有错误的, 但是原题的意思是不知道矩阵的大小的, 所以setZero()里面传递参数的时候不能直接把mat[3][4]传进去的吧....
|
7
jedyu 2014-02-14 14:57:09 +08:00
那你就转成char **, 然后 *(mat + i * j) = ....
|