How to do int func(int size,int matrix[][size])
Author Message
How to do int func(int size,int matrix[][size])

Hello,

I'm trying to write some general matrix functions in C.  I ran into the
following problem.  I checked the FAQ and found nothing, and I can't locate
my copy of K&R.

What I want to do is:

int generate_determinant(int size, int matrix[][size])
{ ... }

Is this possible?

Jim Barbour

Sun, 27 Nov 1994 05:09:45 GMT
How to do int func(int size,int matrix[][size])

Quote:
>Hello,

>I'm trying to write some general matrix functions in C.  I ran into the
>following problem.  I checked the FAQ and found nothing, and I can't locate
>my copy of K&R.

>What I want to do is:

>int generate_determinant(int size, int matrix[][size])
>{ ... }

>Is this possible?

>Jim Barbour

I think we've more or less been through this in the recent C vs. fortran
thread.  The only (?) way to do this in C is by doing your own index
calculations:

int
generate_determinant(int size, int matrix[])
{
...
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
... matrix[i * size + j] ...;

Quote:
}

or something like that.

May give vectorizers fits, although there is an article in the recent
SIGPLAN Conference Proceedings that appears to provide some hope.

--

Sun, 27 Nov 1994 06:34:25 GMT
How to do int func(int size,int matrix[][size])

Quote:
>Hello,
>I'm trying to write some general matrix functions in C.  I ran into the
>following problem.  I checked the FAQ and found nothing, and I can't locate
>my copy of K&R.
>What I want to do is:
>int generate_determinant(int size, int matrix[][size])
>{ ... }
>Is this possible?

mightn't it be just as easy to do

int generate_determinant( int rows, int columns, int *matrix)

?

just an idea...

jim p.

--

Huntsville,AL (205)461-4922
This life is a test. It is only a test. If it had been a real life, you
would have been given instructions....

Sun, 27 Nov 1994 20:13:59 GMT
How to do int func(int size,int matrix[][size])

Quote:

>>int generate_determinant(int size, int matrix[][size])
>>{ ... }
>>Is this possible?
>mightn't it be just as easy to do
>int generate_determinant( int rows, int columns, int *matrix)

I would tend to solve this as outlined below, given my object-oriented
bent...

typedef struct {
int rows, cols;
int *mat;

Quote:
} matrix_t;

...

matrix_t *create_matrix(int rows, int cols)
{
matrix_t *new_mat = malloc(sizeof(matrix_t));

if (new_mat) {
new_mat->mat = malloc(rows * cols * sizeof(int));

if (new_mat->mat) {
new_mat->rows = rows;
new_mat->cols = cols;
}
else {
free(new_mat);
new_mat = NULL;
}
}

return new_mat;

Quote:
}

void destroy_matrix(matrix_t *matrix)
{
free(matrix->mat);
free(matrix);

Quote:
}

int *matrix_ref(matrix_t *matrix, int row, int col)
{
return matrix->mat + row * matrix->cols + col;

Quote:
}

---------------

Given these definitions, a routine passed a matrix_t automatically knows
its dimensions, and the matrix_ref interface means you don't need to roll

...
matrix_t *my_matrix;

my_matrix = create_matrix(5, 10);

*matrix_ref(my_matrix, 3, 4) = 12;

and so forth.  I have left out some obvious error checking, but I'm sure
you get the idea.  I hope this helps!

--

"This sentence is true" : Epiminedes' Paradox -- NOT!
".surivorter erutangis a ma I"

Mon, 28 Nov 1994 01:34:24 GMT
How to do int func(int size,int matrix[][size])

Quote:

>>Hello,
>>I'm trying to write some general matrix functions in C.  I ran into the
>>following problem.  I checked the FAQ and found nothing, and I can't locate
>>my copy of K&R.
>>What I want to do is:
>>int generate_determinant(int size, int matrix[][size])
>>{ ... }
>>Is this possible?
>mightn't it be just as easy to do
>int generate_determinant( int rows, int columns, int *matrix)
>?
>just an idea...
>jim p.

Well, I'm trying to avoid generating my own offsets.  Doing what you
suggest would illiminate constructs like:

matrix[r][c]

and would require

*(matrix + (r * columns) + c)

If I go that route, I'll probably do

int generate_determinant( int size, int matrix[])
...
matrix[(r * size) + c]

Jim B

Mon, 28 Nov 1994 03:51:50 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages