0

Is it possible to make a function that works with arrays of undefined length?
For example, I made this code to fill a matrix of 3x3 and I wonder if is there a way to do this but with a matrix of nxn.

void fillMatrix(double mat[][COLS])
{
    int i,j;    

        printf("Enter the %d matrix elements:\n",COLS*ROWS);
        for(i=0;i<ROWS;i++)
        {
            for(j=0;j<COLS;j++)
            {
                scanf("%lf",&mat[i][j]);
            }                   
        }
        printf("\n");   
}

In this code I defined ROWS=COLS=3.

5 Answers5

2

Yes, if you know the number of columns in the 2D array at the time of passing it to the function. You do not have to define COL beforehand.

void foo(int col, int arr[][col]) {
//Do something
}
P.W
  • 26,289
  • 6
  • 39
  • 76
1

You can try this:

void func(void *data, int row, int col)
{
    int (*a)[col] = (int(*)[col])data;
    //now you can access a[i][j] with i<row and j<col
    //data must be an continous array
    //replace int with your data type
}

Working code:

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

int main()
{
    int n = 5;
    int (*a)[n];
    int b[5][5];

    a = (int(*)[n])b;
    b[0][0]=0;
    b[0][1]=1;
    b[2][1]=111;
    b[1][2]=666;
    b[4][3]=222;

    printf("%d\n", a[0][0]);
    printf("%d\n", a[0][1]);
    printf("%d\n", a[2][1]);
    printf("%d\n", a[1][2]);
    printf("%d\n", a[4][3]);

    return 0;
}
William Taylor
  • 549
  • 3
  • 22
0

may be you can add a new parameter row to your function fillMatrix(double mat[][COLS]), that is, fillMatrix(double mat[][col], int row)

another way:

double** matrix = (double**) malloc(sizeof(double*)*n);
for(int i = 0; i<n; i++){
    matrix[i] = (double*) malloc(sizeof(double)*n);
}

then change the function to: fillMatrix(double** matrix, int n)

shinxg
  • 490
  • 4
  • 9
0

Is it possible to make a function that works with arrays of undefined length?

You'll better know the dimension of the array. If it is a function argument, you should in general pass the dimension in some other argument.

Remember that when passed as argument, an array is decayed into a pointer. Look also into some C reference site and later refer to the C11 standard n1570.

In your case, you want to define then have an abstract data type (probably some opaque pointer) for your matrixes. Using flexible array members could be useful. See this answer for details.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Just saw I can made a function that reserves memory for the array using `malloc` and then I can use that function with my `fillMatrix`. – Alberto Navarro Sep 06 '18 at 22:03
0

yes, you can do it using dinamic memory.

you will something like:

void fillMatrix(double** mat, int rows, int cols)    {
    int i,j;    

    printf("Enter the %d matrix elements:\n",rows*cols);
    for(i=0;i<rows;i++)
    {
        for(j=0;j<cols;j++)
        {
            scanf("%lf",&mat[i][j]);
        }                   
    }
    printf("\n");   
 }

where double** mat is a vector of vectors that you will have to ask for memory using malloc for the rows and the malloc for the columns.

jas-chu
  • 525
  • 5
  • 4