how to get rid of warning (due to passing multidimensional array)

N

nleahcim

Hi - I am working on writing a number of matrix manipulation functions.
The most basic one was a printing algorithm - and it shows the problem
I'm having. I'm passing it a pointer a mutidimensional array (matrix)
as well as the number of rows and columns. Problem is I don't know what
type the pointer to the multi-dimensional matrix should be. Right now
it's an int * and it works just fine, but when I call the function I
get a warning unless I typecast the array as an int *. What's the
proper way to do this? It's designed to work with any sized two
dimensional arrays. This is the code:

void printmatrix(int * matrix, int rows, int cols)
{
unsigned int rowcounter;
unsigned int colcounter;
for (rowcounter = 0; rowcounter < rows; rowcounter++)
{
for (colcounter = 0; colcounter < cols; colcounter++)
printf("%5i ", *(matrix + rowcounter * cols + colcounter));
printf("\n");
}
}

Thanks!

-Mike
 
K

Keith Thompson

Hi - I am working on writing a number of matrix manipulation functions.
The most basic one was a printing algorithm - and it shows the problem
I'm having. I'm passing it a pointer a mutidimensional array (matrix)
as well as the number of rows and columns. Problem is I don't know what
type the pointer to the multi-dimensional matrix should be. Right now
it's an int * and it works just fine, but when I call the function I
get a warning unless I typecast the array as an int *. What's the
proper way to do this? It's designed to work with any sized two
dimensional arrays. This is the code:

void printmatrix(int * matrix, int rows, int cols)
{
unsigned int rowcounter;
unsigned int colcounter;
for (rowcounter = 0; rowcounter < rows; rowcounter++)
{
for (colcounter = 0; colcounter < cols; colcounter++)
printf("%5i ", *(matrix + rowcounter * cols + colcounter));
printf("\n");
}
}

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
Question 6.19, "How do I write functions which accept two-dimensional
arrays when the width is not known at compile time?", seems
particularly relevant, but you should read the whole section (and
eventually the whole FAQ).
 
F

Fred Kleinschmidt

Hi - I am working on writing a number of matrix manipulation functions.
The most basic one was a printing algorithm - and it shows the problem
I'm having. I'm passing it a pointer a mutidimensional array (matrix)
as well as the number of rows and columns. Problem is I don't know what
type the pointer to the multi-dimensional matrix should be. Right now
it's an int * and it works just fine, but when I call the function I
get a warning unless I typecast the array as an int *. What's the
proper way to do this? It's designed to work with any sized two
dimensional arrays. This is the code:

void printmatrix(int * matrix, int rows, int cols)
{
unsigned int rowcounter;
unsigned int colcounter;
for (rowcounter = 0; rowcounter < rows; rowcounter++)
{
for (colcounter = 0; colcounter < cols; colcounter++)
printf("%5i ", *(matrix + rowcounter * cols + colcounter));
printf("\n");
}
}

The above function does NOT use a multi-dimensioned array.
It uses a one-dimensional array that is simulating a 2D array.

The problem is most certainly in the code that you did not
show us, when combined with this code.
When you call it like this:
printmatrix( mat, nr, nc );
what is the declared type of 'mat'? To use the function as
written, 'mat' must be decalred as 'int *' - that is, it
must be a one-dimensional int array of length nr*nc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top