The declaration of a variable multidimensional array like the one below is illegal in C:
double A[z][5]; /* ILLEGAL */
If you wish to create a multidimensional array of variable size, you will have to declare a pointer to an array of pointers and then allocate memory dynamically, as shown below:
double** func (int x, int y)
{
double **A;
int i;
/*allocates dynamic memory and returns a pointer to an array of double pointers*/
A = (double **) malloc( x * sizeof(double *));
/*allocates memory for each element of the array of double pointers*/
for (i = 0; i < x ; i++ )
A[i] = (double *) malloc( y * sizeof(double));
printf("x = %d\n", x);
printf("y = %d\n", y);
/* deallocate the memory */
for (i = 0; i < x ; i++ )
free(A[i]);
free(A);
}
Best Answer