rainversion_3 - 3 years ago 181
C Question

# Adding 2 matrices using pointers

I can acheive it by passing

`(c,a,b)`
to
`add_mat`
function, where result of
`a,b`
is stored in
`c`
like,

`void add_mat(int c[][3], int a[][3], int b[][3], int m, int n)`

What should be the return type of
`add_mat`
if I want to construct the funtion in this way

`?? add_mat(int a[][3], int b[][3], int m, int n)`

Below is a sample code

``````#include<stdio.h>

void read_mat(int a[][3], int m, int n){
//scan data
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("[%d][%d] : ",i,j);
a[i][j]=i+j;
}
}
}
int* add_mat(int a[][3], int b[][3], int m, int n){
int c[3][3];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
return c;
}

int main(){
int a[3][3];
int m=2,n=2; //mxn of a matrix

int b[3][3];
int* c[3][3];

return 0;
}
``````

Like passing or pointing the calculated value
`c`
inside
`add_mat`
function to variable in
`main`
function.

You cannot do that, since the memory of the `c` matrix will be gone when the function terminates.

You need to dynamically allocate it with `malloc()`, in order for the memory not to be free'd, unless you call `free()`. I have some examples for that in 2D dynamic array (C), if you want to take a look.

With your previous function, you would create the matrix `c` outside the function (in `main()`), that's why dynamic memory allocation was not required.

PS: You should compile with warnings enabled:

``````prog.c: In function 'add_mat':
prog.c:19:12: warning: returning 'int (*)[3]' from a function with incompatible return type 'int *' [-Wincompatible-pointer-types]
return c;
^
prog.c: In function 'main':
prog.c:32:7: error: assignment to expression with array type
^
prog.c:31:10: warning: variable 'c' set but not used [-Wunused-but-set-variable]
int* c[3][3];
^
``````

Here is a working example, which is just for demonstrative purposes:

``````#include <stdio.h>
#include <stdlib.h>

void read_mat(int a[][2], int n, int m){
//scan data
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
a[i][j] = i + j ;
}
}
}

int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i, **table;
table = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
table[i] = malloc( M*sizeof(int) );
return table;
}

void print(int** p, int N, int M) {
int i, j;
for(i = 0 ; i < N ; i++)
for(j = 0 ; j < M ; j++)
printf("array[%d][%d] = %d\n", i, j, p[i][j]);
}

void free2Darray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}

int** add_mat(int a[][2], int b[][2], int m, int n){
int** c = get(n, m);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
c[i][j]=a[i][j]+b[i][j];
}
}
return c;
}

int main(){

int n = 2, m = 2; //nxm of a matrix
int a[n][m];

int b[n][m];
int** c;
c = add_mat(a, b, n, m);

print(c, n, m);
free2Darray(c ,n);

return 0;
}
``````

Output:

``````array[0][0] = 0
array[0][1] = 2
array[1][0] = 2
array[1][1] = 4
``````

PPS: If you really want to use static arrays, then I recommend using `int c[3][3]; add_mat(c, a, b, m, n);`

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download