问题描述
所以我写了一个函数,将一个二维数组90度,我已经告诉IRC,我不能通过引用传递二维数组(例如void test(char A [] [10]& )),我只是应该通过我的数组通常的方式,但是当我这样做,这个函数不会改变实际的数组。那么如何在函数中修改我的原始数组呢?
So I've written a function that turns a 2D array 90 degrees and I've been told on IRC that I can't pass 2D array by reference (for example void test(char A[][10]&)) and that I just should pass my array the usual way, however when I do that, this function doesn't change the actual array. So how do I modify my original array in a function ?
void one(char A[][10], int N)
{
char B [10][10];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
B[j][N-i-1] = A[i][j];
A = B;
}
推荐答案
A = B;
不会将数组B的元素永久复制到A.永久更改为 A
元素的无效分配。 A
保留函数返回时的原始值。你需要做一个成员明智的副本永久复制 B
的元素到 A
。
A = B ;
doesn't copy the elements of array B to A permanently. It's an invalid assignment to change to elements of A
permanently. A
retains it's original values upon function return. You need to do a member wise copy to permanently copy the elements of B
to A
.
这篇关于修改函数中的2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!