1

I'm very new to pointers so please bear with me...

My code defines a function for the multiplication of two matrices (matrixMultiplication). I have then defined a function pointer to this function.

#include <iostream>

void matrixMultiplication (const double A[3][3], const double B[3][3], double output[3][3])
{
    int i, j, k; 
    for (i=0;i<3;i++) 
    {
        for(j=0;j<3;j++)
        {
            for(k=0;k<3;k++)
            {
                output[i][j]+=A[i][k]*B[k][j];
            }
        }
    }
}

double (*matrixMultiplication (const double (*left)[3], const double (*right)[3]))[3]
{
    double output[3][3];
    matrixMultiplication(left, right, output);
}

int main ()
{
    using namespace std;

    double A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
    double B[3][3]={{1,1,1},{1,1,1},{1,1,1}};

    cout<<"The function returns..."<<endl;

    double print[3][3]=matrixMultiplication(A,B);

    int i, j;
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            cout<<print[i][j]<<"\t";
        }
        cout<<"\n";
    }
    return 0;
}

What I want to do is output the array given by the pointer function, *matrixMultiplication, using a for loop (just for aesthetic purposes). I have played around with the code and ended up with initialiser or segmentation (11) errors. I feel like I'm missing something blatantly obvious given I'm new to C++...

Any ideas would be most welcome!

4
  • 1
    is this the exact code you used to run? because the given code doesn't even compile for me. Commented Nov 15, 2013 at 12:57
  • Yes, sorry this doesn't compile. The problem like is in the main when introducing 'double print[3][3]'. I have put the for loop into the matrixMultiplication function itself to see if the function pointer produces anything, which it does. Commented Nov 15, 2013 at 13:03
  • 1
    Is there any particular reason you're trying to tackle two different problems -- 2D arrays and function pointers -- at once? Pick one to do first, it'll be easier for everyone. Commented Nov 15, 2013 at 13:05
  • Yeh, it's an assignment I've been given. The first being to create functions for 2D arrays and now the second to use function pointers as an illustration of "code reuse" Commented Nov 15, 2013 at 13:09

2 Answers 2

1

The problem is with:

double (*matrixMultiplication (const double (*left)[3], const double (*right)[3]))[3]
{
  double output[3][3];
  matrixMultiplication(left, right, output);
}

I don't know what it is and neither does my compiler! ;)

Using functional, a matrixMultiplication function type can be defined and used, like so:

#include <functional> // or <tr1/functional>

// type
typedef function<void (const double[3][3], const double[3][3], double[3][3])> MatrixFunction;

// instance
MatrixFunction matrixFunctionPtr(&matrixMultiplication);

// call
matrixFunctionPtr(A,B,print);

Note: you also need to declare your output array double print[3][3]; * before* you call the matrixMultiplication function...

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

Comments

0

You have a function:

void matrixMultiplication (const double A[3][3], const double B[3][3], double output[3][3])
{
  ...
}

This function works. It takes three arrays as arguments (which is to say it takes three pointers-- this is a subtle point, and I don't think it's a good exercise for a beginner because it clouds the distinction between passing by value and passing by reference -- but never mind that for now) and returns void (i.e. nothing), and . Now you want to construct a function pointer that points to this function. But this:

double (*matrixMultiplication (const double (*left)[3], const double (*right)[3]))[3]
{
  ...
}

is not a function pointer; it's a function that returns a pointer to an array of double, but it has some internal errors (and don't even worry about what it takes as arguments for now).

Let's do a simpler example first:

double foo(int n)      // function
{
  return(3);
}

int main()
{
  double (*bar)(int);  // function pointer
  bar = &foo;
  double z = (*bar)(5);
  cout << z << endl;
  return(0);
}

Now that we see how function pointers work, we apply one to matrixMultiplication:

void (*matFP)(const double A[3][3], const double B[3][3], double output[3][3]);
matFP = &matrixMultiplication;
double C[3][3];
(*matFP)(A,B,C);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.