-1

I am using dynamic initialization to generate two Matrices. But problem is that my debugger gives me error in the function "printArray". Can anybody help me why is this happening. I have just learnt dynamic initialization yesterday.

In main function, I take inputs from user about dimensions of matrices and you can observe that on the basis of these inputs I become able to dynamically specify the dimensions of my Matrix.

#include <stdio.h>
#include <stdlib.h>
int Function(int [], int , int);
void printArray(int **Matrix, int row, int column);

int row = 0, col = 0;
int **P = 0;

int main() {


    printf("Give me Dimensions of Matrix 1 : : ");
    printf("No .of Rows : ");
    scanf_s("%d", &row);
    printf("No .of Columns : ");
    scanf_s("%d", &col);

    Function(P, row, col);
    printArray(P, row, col);

    printf("Give me size of Matrix 2 : ");
    printf("No .of Rows : ");
    scanf_s("%d", &row);
    printf("No .of Columns : ");
    scanf_s("%d", &col);
    Function(P, row, col);
    printArray(P, row, col);
    system("pause");
    return 0;
}

int Function(int **A, int s, int c) {
    A = malloc(row * sizeof(int *));
    for (int i = 0; i < row; i++) A[i] = malloc(col * sizeof(int *));

    for (int i = 0; i < s; i++) {

        for (int j = 0; j < c; j++) {

            A[i][j] = rand() % 10;
        }
    }
}


void printArray(int **Matrix, int row, int column) {


    for (int i = 0; i < row; i++) {

        for (int j = 0; j < column; j++) {

            printf("%d ", Matrix[i][j]);
        }

        puts("");
    } 

    puts("");

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

0
  1. In this case the major problem being - C is pass by value and when you passed it to the function, you didn't make any change to the global variable - rather you changed the local variable - local to the called function Function. To make change to it, you need to pass it's address. Then by dereferencing it you are making changes to the actual variable.

Also unless you have something valid to return from function make it of return type void.

For example: (Illustration purpose)

Function(&P, row, col);

...
void Function(int ***A, int s, int c) {
    (*A) = malloc(s * sizeof(int *));
    for (int i = 0; i < s; i++) (*A)[i] = malloc(c * sizeof(int));

    for (int i = 0; i < s; i++) {

        for (int j = 0; j < c; j++) {

            (*A)[i][j] = rand() % 10;
        }
    }

}

And also note

A[i] = malloc(col * sizeof(int *));

will be

(*A)[i] = malloc(col * sizeof(int ));
                             ^^^^^

You need to allocate memory for those int variables which you didn't in earlier case.

  1. Check the return value of malloc and scanf. For example:

    (*A) = malloc(row * sizeof(int *));
    if( (*A) == NULL){
      perror("Malloc failed");
      exit(EXIT_FAILURE);
    }
    

and -

    if( scanf_s("%d", &row)!= 1){
      fprintf(stderrm,"Error in input");
      exit(EXIT_FAILURE);
    }
  1. Inside the function Function (Bad naming I would say), you are using the global variable row. This is bad practice and problematic towards code readability and maintainability.Debugging is much more difficult.
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Function's parameters are its local variable. You can imagine your function definition and its call the following way

int **P = 0;
Function(P, row, col);

//...

int Function( /*int **A, int s, int c*/) {
    int **A = P;
    int s = row;
    int c = col;

    A = malloc(row * sizeof(int *));
    //...
}

So as it is seen it is the function parameter A that is changed in the function. The original argument P is not changed in the function. That is the function deals with a copy of the original argument.

You should either pass the argument by reference like

int **P = 0;
Function( &P, row, col);

//...

int Function( int ***A, int s, int c) {
    *A = malloc(row * sizeof(int *));
    //...
}

or return from the function the created pointer. For example

int **P = 0;
P = Function( row, col);

//...

int ** Function( int s, int c) {
    int **A = malloc(row * sizeof(int *));
    //...
    return A;
}

Take into account that the function has return type int but returns nothing.

Also it is a bug that instead of the parameters the function uses the gobal variable row within the function as for example

for (int i = 0; i < row; i++) A[i] = malloc(col * sizeof(int *));
                ^^^^^^

Also you should free allocated memory. Otherwise there are memory leaks in the program.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335