1

I need to create a matrix calculator, for that I think the best is to create functions for +,-,*,/.

But a lot of troubles come with this idea.

I create a calloced array like:

int **matrix = NULL;

matrix=calloc(cols,sizeof(int*));
for(int i=0;i<cols;i++) {
    matrix[i]=calloc(rows,sizeof(int*));
}

For now I want to create a function where I want to work with this array.

void addition(int **array,int rows, int cols){
    // ** some algoritm here**
    return (the result of addition stored in 2D array);
}

I also tried to google the problem, but I didn't underestand the solutions. What I need to explain the way how to pass the array to function, just for reading.

After that I need to return some pointer to new array created as the result of addition. I will probably create the new array in the function.

But If I wanted to write something in array created in main, how to pass and use it in the function?

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • Possible duplicate: [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Lundin Mar 26 '19 at 11:36
  • you calloc using sizeof(int*) twice, whereas 1 should be just a regular int. Unless, matrix pointer artimethic is also an issue :-) – hetepeperfan Mar 26 '19 at 11:36
  • 1
    matrix[i] = calloc(rows, sizeof(int*)); does not look right. – machine_1 Mar 26 '19 at 11:54
  • `int (*p)[rows][cols] = malloc(sizeof *p)` will allocate memory for a 2D array, yet with `int **matrix`, it appears you want something else. – chux - Reinstate Monica Mar 26 '19 at 12:38

0 Answers0