3 Replies - 2096 Views - Last Post: 08 November 2011 - 10:17 AM Rate Topic: -----

#1 u2v22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-November 11

Using pointers to return a 2D matrix from a function

Posted 08 November 2011 - 08:51 AM

I would like to return an array from a function in C++. I soon realized I couldn't do this, I need pointers. I'm not very familiar with pointers (at all) and would appreciate a little direction/help. My code is below.

This is the function in the class (the actual code is much longer so I just included sections). I want to be able to use the multiply code later in my main function in addition to this one instance.

double multiply(int N, double mat[][], double rix[][])
// This matrix multiplies two square matrices
{
	int    i, j, k, z ;
	double sum, product ; 

	for (i=0; i<2; i++){
		for (j=0; j<2; j++){
			sum = 0;
       		for (k=0; k<2; k++){
			sum = sum + mat[i][k] * rix[z][j]; }
		*(product[i][j]) = sum; }}
	return product ;
}



The main function is:
// ...
	double *kt_ptr, kt[2][2], ktt[2][2] ;
	*kt_ptr = &kt[][] ;
        double kt[2][2] = {{3, 5},{1, 9}};
        double  M[2][2] = {{1, 8},{4, 6}};
        ktt = &multiply(2, M, kt);
// ...



Is This A Good Question/Topic? 0
  • +

Replies To: Using pointers to return a 2D matrix from a function

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4154
  • View blog
  • Posts: 12,941
  • Joined: 25-December 09

Re: Using pointers to return a 2D matrix from a function

Posted 08 November 2011 - 09:22 AM

In the following snippet:
	double sum, product ; 

	*(product[i][j]) = sum; }}

You have declared product to be a single double, yet in the second line you are trying to use it like it was an array.
Also in the following line:
double multiply(int N, double mat[][], double rix[][])
// This matrix multiplies two square matrices
{

You must specify the size of all but the first element of the arrays, double mat[][2].

What exactly are you trying to return from this function?

There are also several errors in your "main" function. You are defining several variables twice.

Also I recommend that you read the tutorials provided in my signature. They should help explain how to pass arrays into functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 u2v22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-November 11

Re: Using pointers to return a 2D matrix from a function

Posted 08 November 2011 - 09:59 AM

Thanks for that feedback Jim.

I just realized product should actually be product[][](a 2D array) in the declaration. Silly mistake. I looked at your tutorial for passing functions but I'm not trying to pass the pointer into the function, I'm trying to return it.

The main goad of the is to take two matrices, multiply them, and then return the product -> [A]*[B] = [C].

My two inputs A and B don't need to be pointers, it's my matrix C which I am trying to return, and therefore which I need a pointer to go to (but it's not an input into the function). This is the main source of all my confusion. Or maybe I'm going about this the wrong way.

In this specific instance all the inputs are 2by2 matrices but in other instances the matrices are larger.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4154
  • View blog
  • Posts: 12,941
  • Joined: 25-December 09

Re: Using pointers to return a 2D matrix from a function

Posted 08 November 2011 - 10:17 AM

Two problems
1.) You can not return a pointer to a "local" variable.
2.) You can not return an array from a function with return, you can only return a pointer.

There are a couple of ways to "return" an array from a function, the first is to pass a parameter into the function to use to return the array. When you pass an array into a function any changes made to that array in the function will be reflected in the calling function after the function returns. The other ways require the use of using a static variable, or using dynamic memory.

Jim

This post has been edited by jimblumberg: 08 November 2011 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1