简体   繁体   English

将指向数组的指针传递给函数

[英]Passing a pointer to an array of arrays to a function

I have a main function which sets up the following variables: 我有一个设置以下变量的主要功能:

double matrix[numVectors][size] = {
    {0.183963, 0.933146, 0.476773, 0.086125, 0.566566, 0.728107, 0.837345, 0.885175, 0.600559, 0.142238},
    {0.086523, 0.025236, 0.252289, 0.089437, 0.382081, 0.420934, 0.038498, 0.626125, 0.468158, 0.247754},
    {0.969345, 0.127753, 0.736213, 0.264992, 0.518971, 0.216767, 0.390992, 0.242241, 0.516135, 0.990155}
};

double result1[size], result2[size];

double *ptr_matrix  = &matrix[0];
double *ptr_result1 = &result1[0];
double *ptr_result2 = &result2[0];

What the above is trying to do is: 上面要尝试做的是:

  • Create an array with three rows of 10 doubles 创建一个三行包含10个双打的数组
  • Create two empty arrays of 10 doubles 创建两个包含10个double的空数组
  • Create a pointer to the matrix 创建一个指向矩阵的指针
  • Create pointers to the two empty arrays 创建指向两个空数组的指针

Then, I'm trying to pass all three pointers to another function. 然后,我试图将所有三个指针传递给另一个函数。 This other function will iterate over the matrix rows (only the rows, it doesn't visit the whole matrix space), perform a computation using the row (as an array). 另一个函数将遍历矩阵行(仅行,它不访问整个矩阵空间),使用该行(作为数组)执行计算。 The end result is the two empty arrays declared at the beginning end up each becoming one row from the matrix. 最终结果是在开始处声明的两个空数组结束,每个空数组都从矩阵变成一行。

Here is the prototype of the second function: 这是第二个函数的原型:

void smallestSum(double (*mat)[size], int num, double *first, double *second)

This function goes through each combination of the matrix rows (0/1, 0/2, 1/2) and checks the sums of their values. 此函数遍历矩阵行的每种组合(0 / 1、0 / 2、1 / 2),并检查其值的总和。 The two arrays producing the smallest sum eventually become result1 and result2 from above. 产生最小总和的两个数组最终从上方变成result1result2

Seeing as this is the first time I'm really delving into pointer/array/matrix territory, I have a few questions: 看到这是我第一次真正研究指针/数组/矩阵领域,所以我有几个问题:

  • Am I correctly "getting" a pointer to the matrix? 我是否正确“获取”了指向矩阵的指针? Or do I need to get a pointer to the first value of the matrix instead? 还是我需要获取一个指向矩阵第一个值的指针?
  • In smallestSum() , can I iterate over the array as I would normally (using for (int i = 0; i < num; i++) ? smallestSum() ,是否可以像往常一样遍历数组( for (int i = 0; i < num; i++)

You need to change the definition of ptr_matrix , as it's not a pointer to a single double, but to the whole row: 您需要更改ptr_matrix的定义,因为它不是指向单个double的指针,而是指向整个行的指针:

double (*ptr_matrix)[size] = &matrix[0];

Then, you can call the function as follows: 然后,您可以按以下方式调用该函数:

smallestSum(ptr_matrix, numVectors, ptr_result1, ptr_result_2);

Inside smallestSum , you can iterate both over rows and over columns. smallestSum内部,您可以遍历行和遍历列。

Note that size must be known at compilation time. 请注意,必须在编译时知道size

If the function doesn't modify the matrix, consider adding const to the type of its first argument. 如果函数没有修改矩阵,请考虑将const添加到其第一个参数的类型。

The answer abacabadabacaba gave is mostly correct except that size does not need to be known at compile time. abacabadabacaba给出的答案基本上是正确的,只是在编译时不需要知道size If you include size as a parameter to the function you can use it as part of the type for other parameters to that function: 如果将size用作函数的参数,则可以将size用作该函数其他参数的类型的一部分:

void smallestSum(int size, double (*mat)[size], int num, double *first, double *second)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM