[comp.lang.c] Pb with passing a row of a matrix to a function

olivier@alpo.colorado.edu (04/13/90)

I am trying to write a function which takes as argument
a pointer to a row of a matrix:

f(row, dim)
int *row;
	{
		stuff with row[j]; ...

where f is called as f(&matrix[i]) and 
matrix has been declared as two-dimensional 
(int **matrix , calloc-ed and initialized)
In other words, I'd like row[j] to  be equal to  matrix[i][j];
This doesn't work because, in f, row points to matrix[i] which is a pointer
to an int (matrix[i][0]), and not an int. I can't do the extra redirection in f,
though.  Any hints on what I could do, or is there something wrong in my 
reasoning?

Thanks,
Olivier
	
Olivier Brousse                       |
Department of Computer Science        |  olivier@boulder.colorado.EDU
Institute of Cognitive Science        |
U. of Colorado, Boulder               |

brnstnd@stealth.acf.nyu.edu (04/15/90)

In article <19604@boulder.Colorado.EDU> olivier@alpo.colorado.edu () writes:
  [ function f(row) int *row ... takes as an argument a ``pointer to a ]
  [ row of a matrix''; f(&matrix[i]) is wrong type with int **matrix ]

You've correctly analyzed the problem. To see the solution, think about
the use of row. row is a pointer to integers; *row, *(row + 1), and so
on are the elements of that row. You want that row to be a row of your
matrix, i.e., the elements matrix[i][j] == *(*(matrix + i) + j) for j
0, 1, and so on. You want *(row + j) to be *(*(matrix + i) + j); so row
should equal *(matrix + i), i.e., matrix[i].

In other words: f(matrix[i]), just as in any other competent language.

By the way, you should describe row as ``a pointer to the elements of a
row of a matrix,'' not ``a pointer to a row of a matrix.''

---Dan