0

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
  • Create two empty arrays of 10 doubles
  • 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. The two arrays producing the smallest sum eventually become result1 and result2 from above.

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++)?
1
  • Use vector<vector<double>> then to pass to your function, vector<vector<double>>& v voila! Commented Sep 23, 2014 at 0:03

2 Answers 2

1

You need to change the definition of ptr_matrix, as it's not a pointer to a single double, but to the whole row:

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.

Note that size must be known at compilation time.

If the function doesn't modify the matrix, consider adding const to the type of its first argument.

Sign up to request clarification or add additional context in comments.

5 Comments

Sweet, that worked. Makes much more sense. However, when iterating over the matrix, how would I dereference one of the rows to get the actual value of the row? I currently have (*mat)[0] (for the first row), but that's giving me a type of double *, whereas I want a double[].
@MaxMackie You may use double * value as if it were double[]. To access a cell in the matrix, you can write mat[i][j] (i is the row index, and j is the column index).
Maybe I'm misrepresenting the error then. Within that function, I call another function to process two arrays (rows in the matrix). The prototype for that second function is second(double a1[], double a2[]). I figured that double * was causing a conflict with double[]. Is that wrong?
@MaxMackie A function parameter of type double[] is the same thing as a parameter of type double *. I think the problem is that you think that (*mat)[0] is the first row, but actually it is the first element of the first row. The first row is *mat or mat[0].
Ahhh, I though you had to dereference the mat pointer to get to the first row. That makes much more sense. Thanks for filling the gaps!
0

The answer abacabadabacaba gave is mostly correct except that size does not need to be known at compile time. 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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.