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.

--
Larry Meadows           The Portland Group



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
your own address calculations.  A typical use might look like:

  ...
  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  
 
 [ 5 post ] 

 Relevant Pages 

1. int func(int) versus int func(int *) efficacy.

2. (int/int) != int

3. Calling int f(int (*f)(int)) like function in DLL from VB

4. URGENT: int func( int t, int k, char s[t][k] ) right ?

5. extern int foo(int); vs int foo(int);

6. using const int to set static array's storage size

7. DJGPP reserves wrong int size

8. Size of int and long

9. size_t vs int size

10. Size of an int?

11. Different sizes of long int on different machines.

12. Standard int sizes

 

 
Powered by phpBB® Forum Software