1

Hello I had to write a program (well still have) that would allocate memory in function for storing numbers that you have to input then print a matrix (rows and columns are the same size). Most importantly the program has to be written using pointers, local variables, functions and C 89 standard.

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

    void Matrix_Input(int *m, int ***Matrix);
    void Matrix_Output(int m, int **Matrix);

    int main()
    {
        int m;
        int **Matrix;
        int i;
        Matrix_Input(&m, &Matrix);
        Matrix_Output(m, Matrix);

        for (i = 0; i < m; i++) /*free memory*/
            free(*(Matrix+i));
        free(Matrix);
        return 0;
    }
    void Matrix_Input(int *m, int ***Matrix)
    {
        int i, j;
        printf("Input number of rows/columns: \n");
        scanf("%d", m);
        *Matrix = malloc(*m* sizeof(int*)); /*allocate memory*/
        for (i = 0; i < *m; i++)
            *(*Matrix+i) = malloc(*m* sizeof(int));
        printf("Input integers: \n");
        for (i = 0; i < *m; i++)
            for (j = 0; j < *m; j++)
                scanf("%d", &((*Matrix)[i][j]));
    }
    void Matrix_Output(int m, int **Matrix)
    {
        int i, j;
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < m; j++)
                printf("%5d", Matrix[i][j]);
        printf("\n");
        }
    }

The program works fine, but I was asked not to use triple pointers here(for input function):

    void Matrix_Input(int *m, int ***Matrix)

Teacher told me to use double pointers for input function just like I did for output like this:

    void Matrix_Input(int *m, int **Matrix)

And this is where everything goes wrong since I only know how to allocate with triple pointers. I have to leave input as a separate function, can't put it in main.

Could someone help me out? Please.

alk
  • 69,737
  • 10
  • 105
  • 255
  • "*use double pointers for input function just like I did for output*": If not returning the double pointer as function value (as pointed out by [Matthieu's answer](https://stackoverflow.com/a/52907523/694576)) then this is only possible if you let the caller of the function allocate the array, or at least its 1st dimension. – alk Oct 20 '18 at 16:52
  • It makes me cringe when people are forced to learn to code to an almost 30-year old standard. The software industry is supposed to be dynamic and continually improving things. Continuing to teach C89 is, frankly, close to ludicrous. – Jonathan Leffler Oct 20 '18 at 17:31
  • Can't disagree there. I was quite sad when we were told to use c89, makes learning harder since there is less relevant information on the Internet. Maybe that's the point, just make it harder. – Juana Thomas Oct 20 '18 at 17:41
  • You are aware, that what you call a 2D NxN array in fact are 1+N 1D arrays of dimension N, aren't you? – alk Jan 20 '19 at 16:21

1 Answers1

0

Return your Matrix pointer instead. It's an output to the function, not a real input.

int** Matrix_Input(int* n) 
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62