Home > Enterprise >  Trouble passing a 2d array to function after malloc
Trouble passing a 2d array to function after malloc

Time:01-19

I've gotten back into C recently after a long absence, and I can't for the life of me remember how to pass a 2D matrix to a function and use it after mallocating it. I believe I've passed the pointer correctly, but I can't seem to actually access anything in the matrix and can't figure out why.

This is what I have written:

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

void matrixOperation(int *arr, int m, int n)
{
    
    printf("in matrixOperation\n ");
    int i, j;
    for (i = 0; i < m; i  )
      for (j = 0; j < n; j  )
        printf("%d\n", *((arr i*n)   j));
}
 
int main()
{
    int i,j,count;
    int row, col;
    
    
    //------------------------------
    printf("Number of rows?\n  ");
    scanf("%d", &row);
    
    printf("Number of columns?\n  ");
    scanf("%d", &col);
    
    //------------------------------
    
    
    int* arr[row];
    for (i = 0; i < row; i  )
        arr[i] = (int*)malloc(col * sizeof(int));
    
    count = 0;
    for (i = 0; i < row; i  )
        for (j = 0; j < col; j  )
            arr[i][j] =   count;
 
 
 for (i = 0; i < row; i  )
        for (j = 0; j < col; j  )
            printf("%d\n",arr[i][j]);
 
 
 
    // We can also use "print(&arr[0][0], m, n);"
    matrixOperation((int *)arr, row, col);
    
    for (int i = 0; i < row; i  )
        free(arr[i]);
    
    
    return 0;
}

The goal was to have it accept user input for the size of the matrix, fill each index with a count and then pass it to a function which would print it out. However when I try it the print statement just outputs random numbers which I assume are what the pointers are pointing at.

I would like matrixOperation to print out the same thing as the print statement in main and I can't quite figure out what I've done wrong.

CodePudding user response:

Change the function signature and you should be able to use the same piece of code.

void matrixOperation(int **arr, int m, int n)
{
    for (i = 0; i < m; i  )
        for (j = 0; j < n; j  )
            printf("%d\n",arr[i][j]);
}

Call with:

matrixOperation(arr, row, col);

Remember when an array is passed to a function, it "decays" into a pointer. You have an array of pointers. That decays to a pointer to pointers.

CodePudding user response:

You declared an array of pointers

int* arr[row];

So pass this array to the function.

The function will be declared like

void matrixOperation(int **arr, int m, int n)
{
    
    printf("in matrixOperation\n ");
    int i, j;
    for (i = 0; i < m; i  )
      for (j = 0; j < n; j  )
        printf("%d\n", *( *( arr   i )   j ) );
}

and call the function like

matrixOperation( arr, row, col );

Though it will be better to define the function like

void matrixOperation(int **arr, int m, int n)
{
    printf("in matrixOperation\n ");

    for ( int i = 0; i < m; i   )
    {
        for ( int j = 0; j < n; j   )
        {
            printf("%d ", *( *( arr   i )   j ) );
        }
        putchar( '\n' );
    }
}

As for your call of the function with casting the array designator

matrixOperation((int *)arr, row, col);

then it is wrong because you do not have one extent of memory. You allocated several extents of memory.

  • Related