# Using pointers to return a 2D matrix from a function

Page 1 of 1

## 3 Replies - 3125 Views - Last Post: 08 November 2011 - 10:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254826&amp;s=5c84106926d637117772ddb41f359830&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 u2v22

Reputation: 0
• 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

Reputation: 4734
• Posts: 14,759
• 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

### #3 u2v22

Reputation: 0
• 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.

### #4 jimblumberg

Reputation: 4734
• Posts: 14,759
• 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }