473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Dec 17 '06 #1
2 1698
"nl******@gmail.com" <nl******@gmail.comwrites:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 17 '06 #2

<nl******@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Dec 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: WindAndWaves | last post by:
Hi Folks I know that this is possible: Function tt(x as string, nv()) where NV is an array. What I am wondering is if NV can be a multidimensional array.
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
6
by: subramanian | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
2
by: nitinm | last post by:
hi I want to make a program whose requirement are as following: 1) it has to create an NxN matrix after reading input (i.e. N) from a file in the main() itself. 2) it has to send the array as...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.