# 将函数的静态数组返回到main（）函数中的另一个数组

``````const int nRows = 2;
const int nCols = 2;
int * colSum (int [nRows][nCols]);
int * rowSum (int [nRows][nRows]);

int main() {

int my2Darray[nRows][nCols] = {{10, 20}, {30, 40}};
int totalsByColumn[nCols] = {};
*totalsByColumn = *(colSum(my2Darray));

for (int i = 0; i < nCols; i++) {
cout << totalsByColumn[i] << endl;
}
}

int * colSum (int arrayArg[nRows][nCols]) {

static int arr[nRows] = {};

for (int i = 0; i < nCols; i++) {
for (int rowcount = 0; rowcount < nRows; rowcount++) {
arr[i] += arrayArg[rowcount][i];
}
}
return arr;
}
``````

``````int main() {

int my2Darray[nRows][nCols] = {{10, 20}, {30, 40}};
int *totalsByColumn = colSum(my2Darray);

for (int i = 0; i < nCols; i++) {
cout << totalsByColumn[i] << endl;
}
}
``````

``````int* colSum (int arrayArg[][nCols], int out[]) {
for (int i = 0; i < nCols; i++) {
out[i] = 0;
for (int rowcount = 0; rowcount < nRows; rowcount++) {
out[i] += arrayArg[rowcount][i];
// ...

int totalsByColumn[nCols];
colSum(my2Darray, totalsByColumn);
``````

0条评论