路过的MultiArrays成函数指针通过 [英] Passing multiarrays into functions through pointer

查看:90
本文介绍了路过的MultiArrays成函数指针通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以传递到多阵列功能,通过指针与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存储器地址,...和(请参阅下表)。

10被保存在的0x30存储器地址

  *内存*存储器地址=>值=>在这个内存地址指针指向   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 我* 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 the array[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 the array[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 after a[0][0]. So pt 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 move i * COLUMN_SIZE elements away in order to get in the correct row where the element is (each row has COLUMN_SIZE elements) and then you need to add j in order to get in the correct column.

If you want to get the a[2][1] (where COLUMN_SIZE = 3), then 2 * COLUMN_SIZE = 6 + 1 = 7. So in order to get the a[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆