路过的MultiArrays成函数指针通过 [英] Passing multiarrays into functions through pointer
问题描述
我怎么可以传递到多阵列功能,通过指针与C ++。我可以简单数组做到这一点。
无效美孚(INT * ARR){}
INT someArr [10];
美孚(someArr);
怎么样二维数组?
您可以实现你这样想:
无效美孚(INT *数组){}INT COLUMN_SIZE = 5;诠释主(){
int数组[COLUMN_SIZE] [2]; 富(安培;阵列[0] [0]); 返回0;
}
虽然你应该采取你如何阅读从富
函数中的元素照顾。
为了阅读数组[C] [R]
元素,你必须做到:
INT元素= *(数组+ C * COLUMN_SIZE + R);
一般获取元素的功能是:
INT get_element为(int *阵列,诠释行,诠释列,诠释COLUMN_SIZE){
INT元= *(数组+行* COLUMN_SIZE +列);
归元;
}
所以,如果你让我们说一个二维数组,如int 数组[M] [N]
,你想要得到的数组[我] [J]
元素,你只需要调用该函数是这样的:
getElement(安培;阵列[0] [0],I,J,N)
为什么这项工作?
究其原因,如果你知道如何阵列2D保存在内存上面的函数作品可以澄清。该阵列保存一行接一行,让我们说你有下面的数组:
int类型的[3] [3] = {{1,2,4},{5,6,7},{8,9,10}};
假设的整数为4字节和&放大器;一个[0] [0]对应为0x10存储器地址。
然后1被保存在为0x10存储器地址,2保存在0x14的,...,7被保存在0X24存储器地址,...和(请参阅下表)。
*内存*存储器地址=>值=>在这个内存地址指针指向 0×10 => 1 => &放大器;一个[0] [0]
0×14 => 2 => (放大器;一个[0] [0] + 1)或(&放大器;一个[0] [1])
为0x18 => 4 => (放大器;一个[0] [0] + 2)或(&放大器;一个[0] [2])
为0x1c => 5 => (放大器;一个[0] [0] + 3 * 1 + 0)或(&放大器;一个[1] [0])
为0x20 => 6 => (放大器;一个[0] [0] + 3 * 1 + 1)或(&放大器;一个[1] [1])
0X24 => 7 => (放大器;一个[0] [0] + 3 * 1 + 1)或(&放大器;一个[1] [2])
0x28 => 8 => (放大器;一个[0] [0] + 3 * 2 + 0)或(&放大器;一个[2] [0])
0x2c => 9 => (放大器;一个[0] [0] + 3 * 2 + 1)或(&放大器;一个[2] [1])
的0x30 => 10 => (放大器;一个[0] [0] + 3 * 2 + 2)或(&放大器;一个[2] [2])
块引用>现在,当你有以下指针:
为int * PT =(安培; A [0] [0] + 2);
在
PT
指针将指向后2的元素a [0] [0]
。因此,PT
在指向一个[0] [2]。* PT
将等于4。现在假设您想要得到的
A [I] [J]
元素。为了得到这个元素
你需要以正确的行所在的元素是获得(每行有COLUMN_SIZE $ C $移动
我* COLUMN_SIZE
元素远C>元素),然后你需要添加Ĵ
,才能在正确的列就搞定了。如果你想获得的
A [2] [1]
(其中COLUMN_SIZE = 3
),然后2 * COLUMN_SIZE = 6
+1
=7
。因此,为了获得A [2] [1]
元素你做*(&安培; A [0] [0] + 2 * 3 + 1)
或*(&安培; A [0] [0] + 7)
。有关指针一些伟大的教程,在这里看看:斯坦福CS埃德图书馆
how can I pass multiarray into function through pointer with c++. I can do this with simple arrays
void Foo(int *arr) { } int someArr[10]; Foo(someArr);
What about 2-dimensions array?
解决方案You can achieve what you want in this way:
void foo(int *array) { } int column_size = 5; int main() { int array[column_size][2]; foo(&array[0][0]); return 0; }
although you should take care on how you read the elements from inside the
foo
function. In order to read thearray[c][r]
element you must do:int element = *(array + c * column_size + r);
The general get element function is:
int get_element(int *array, int row, int column, int column_size) { int element = *(array + row * column_size + column); return element; }
So, if you have let's say an 2D array like int
array[M][N]
and you want to get thearray[i][j]
element you just call the function in this way:getElement(&array[0][0], i, j, N)
Why does this work?
The reason the above function works can be clarified if you know how 2D arrays are saved in memory. The arrays are saved row-by-row, so let's say you have the following array:
int a[3][3] = {{1, 2, 4}, {5, 6, 7}, {8, 9, 10}};
let's assume an integer is 4 bytes and &a[0][0] corresponds to 0x10 memory address. Then 1 is saved in 0x10 memory address, 2 is saved in 0x14, ..., 7 is saved in 0x24 memory address, ... and 10 is saved in 0x30 memory address (see the following table).
*Memory* Memory address => Value => Pointer pointing at this memory address 0x10 => 1 => &a[0][0] 0x14 => 2 => (&a[0][0] + 1) or (&a[0][1]) 0x18 => 4 => (&a[0][0] + 2) or (&a[0][2]) 0x1c => 5 => (&a[0][0] + 3 * 1 + 0) or (&a[1][0]) 0x20 => 6 => (&a[0][0] + 3 * 1 + 1) or (&a[1][1]) 0x24 => 7 => (&a[0][0] + 3 * 1 + 1) or (&a[1][2]) 0x28 => 8 => (&a[0][0] + 3 * 2 + 0) or (&a[2][0]) 0x2c => 9 => (&a[0][0] + 3 * 2 + 1) or (&a[2][1]) 0x30 => 10 => (&a[0][0] + 3 * 2 + 2) or (&a[2][2])
Now when you have the following pointer:
int *pt = (&a[0][0] + 2);
the
pt
pointer will be pointing 2 elements aftera[0][0]
. Sopt
is pointing at a[0][2].*pt
will be equal to 4.Now let's say you want to get the
a[i][j]
element. In order to get this element you need to movei * COLUMN_SIZE
elements away in order to get in the correct row where the element is (each row hasCOLUMN_SIZE
elements) and then you need to addj
in order to get in the correct column.If you want to get the
a[2][1]
(whereCOLUMN_SIZE = 3
), then2 * COLUMN_SIZE = 6
+1
=7
. So in order to get thea[2][1]
element you do*(&a[0][0] + 2 * 3 + 1)
or*(&a[0][0] + 7)
.For some great tutorials on pointers, have a look here: Stanford CS Ed Library.
这篇关于路过的MultiArrays成函数指针通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!