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.

2
  • this one void fillMatrix(double **mat, int *row, int *col); Commented Sep 6, 2018 at 5:28
  • Functions never take arrays as arguments. Arrays are decayed into pointers when passed as argument in a function call. See some C reference site. Commented Sep 6, 2018 at 7:44

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

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)

Comments

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.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.