473,320 Members | 1,865 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,320 software developers and data experts.

two-dimensional array

Hi,
I'm having some problems working with two-dimensional arrays. Here's
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.
I want to work with them inside a for loop, but depending on the
iteration, one is the real one and the other is dummy. At the end of
the iteration I swap them.
I'm working with pointers but it is'nt working as assignments in lines
15 and 16 are from different types. I'm pretty sure I've been through
this before but can't remember how I solved it. Any help? Here's the
code I've got:
-----------------
#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/

for (i=0; i < MAXITERS; i++){
/*Work with Matrix*/
/*...*/
/*Swap Matrix*/
swapMatrix=realMatrix;
realMatrix=dummyMatrix;
dummyMatrix=swapMatrix;
}

return 0;
}

Mar 4 '06 #1
17 2171
my**************@gmail.com wrote:

#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/


test.c:15: warning: assignment from incompatible pointer type
test.c:16: warning: assignment from incompatible pointer type

The correct declarations are:

int (*realMatrix)[COLS];
int (*dummyMatrix)[COLS];
int (*swapMatrix)[COLS];

-Larry Jones

Hmm... That might not be politic. -- Calvin
Mar 4 '06 #2
The declaration for the pointers should be:
int (*realMatrix)[COLS];
Remember, although an array is automatically converted to a pointer to
the first element in expressions, it is fundamentally different from a
pointer.
IIRC, this is one of the innovations made to turn the older "B"
programming language into "C".
For example, the declaration "int a[5];" creates room for five
integers, but does not store a pointer to the first integer anywhere in
memory. Therefore, you can't point to an array with a pointer to a
pointer ("int **p; p=&a;"), because a pointer to a pointer must point
to a pointer physically present in memory, but "int a[5];" does not
create a pointer physically present in memory.
Thus, you must use a pointer to an array ("int (*p)[5]; p=&a;"), which
recognizes that you are not pointing to a pointer, but rather to an
array. You are physically pointing to the first element, and when you
dereference it, you get an array, which is automatically converted to a
pointer to the first element. Therefore, ironically (void *)p==(void
*)*p
Note that this does not invalidate "int a[5]; int *p; p=a;", as this
expression derives a pointer to the first element of a automatically,
and assigns it to p.
I hope this wasn't too confusing to you, because I barely understand it
and wish someone more familiar with ANSI C could explain it.
Good luck!
Jimmy Hartzell

Mar 4 '06 #3
my**************@gmail.com writes:
Hi,
I'm having some problems working with two-dimensional arrays. Here's
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.
No, the correct spelling would be matrices.
I want to work with them inside a for loop, but depending on the
iteration, one is the real one and the other is dummy. At the end of
the iteration I swap them.
I'm working with pointers but it is'nt working as assignments in lines
15 and 16 are from different types. I'm pretty sure I've been through
this before but can't remember how I solved it. Any help? Here's the
code I've got:
-----------------
#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
These pointers are all the wrong type.

matrix1 and matrix2 are both {arrays of /ROWS/ array of /COLS/ ints}.
The type of matrix1[0] is {array of /COLS/ ints}. If the size of an
int is 4 bytes, then the size of matrix1[10] is COLS x 4 == 200 bytes.

realMatrix, etc., are all of type {pointer to pointer to int}.
The type of realMatrix[0] is {pointer to int}, which has whatever size
a pointer to int might have (*very* unlikely to be 200 bytes).
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/
And here you run into trouble. First of all, no C implementation will
let you do this without at least complaining about it. You'd need to
cast it (but don't). After making these assignments, then trying to
access something using realMatrix[n] or realMatrix[n][m] will give you
undefined behavior. Because realMatrix[n] thinks it's dereferencing a
pointer to a pointer to int, when in reality it's dereferencing a
converted pointer to /array/ of int.

If you declare realMatrix, etc, as:

int (*realMatrix)[COLS]; /* pointer to array of COLS ints. */

things are much more likely to behave the way you expect them to,
although, since you didn't post the actual code that works with these
matrices, I can't know that for certain.

for (i=0; i < MAXITERS; i++){
/*Work with Matrix*/
/*...*/
/*Swap Matrix*/
swapMatrix=realMatrix;
realMatrix=dummyMatrix;
dummyMatrix=swapMatrix;
}

return 0;
}

Mar 4 '06 #4
my**************@gmail.com writes:
I'm having some problems working with two-dimensional arrays. [snip] int matrix1[ROWS][COLS]; [snip] int **realMatrix; [snip] realMatrix=matrix1; /*Line 15*/


Arrays are not pointers, and pointers-to-pointers are not useful in
dealing with true 2-dimensional arrays. (Multidimensional arrays can
also be implemented, or perhaps I should say emulated, using pointers
to pointers, but that's not what you're doing here.)

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
(Consider reading the whole thing while you're at it.) If you're
still confused after that, feel free to come back with more questions.

--
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.
Mar 4 '06 #5
Micah Cowan wrote:
my**************@gmail.com writes:
Hi,
I'm having some problems working with two-dimensional arrays. Here's
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.


No, the correct spelling would be matrices.


From Merriam-Webster:

Main Entry: ma·trix
Pronunciation: 'mA-triks
Function: noun
Inflected Form(s): plural ma·tri·ces /'mA-tr&-"sEz, 'ma-/; or
ma·trix·es /'mA-trik-s&z/

Brian
Mar 5 '06 #6
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
No, the correct spelling would be matrices.


From Merriam-Webster:

ma·trix·es /'mA-trik-s&z/


We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 5 '06 #7
Mark McIntyre said:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
No, the correct spelling would be matrices.


From Merriam-Webster:

ma·trix·es /'mA-trik-s&z/


We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


You'd better get on to Chambers, too, then. They list -ices and -ixes.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 5 '06 #8
On Sun, 05 Mar 2006 11:58:54 +0000, Mark McIntyre wrote:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
No, the correct spelling would be matrices.
From Merriam-Webster:
ma·trix·es /'mA-trik-s&z/


We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


And no hint of irony! If you do write, cc both Collins and the OED
as they have also perpetuated this error (the shorter OED has the audacity
to list the wrong spelling first). ;-)

Mark McIntyre


--
Ben.
Mar 5 '06 #9
On Sun, 5 Mar 2006 13:39:15 +0000 (UTC), in comp.lang.c , Richard
Heathfield <in*****@invalid.invalid> wrote:
Mark McIntyre said:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:

No, the correct spelling would be matrices.

From Merriam-Webster:

ma·trix·es /'mA-trik-s&z/


We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


You'd better get on to Chambers, too, then. They list -ices and -ixes.


I never said which one was right :-)

(FWIW I prefer the european -ices over the atlantic -ixes, but the
latter is probably acceptable as long as one is consistent).
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 5 '06 #10
On Sun, 05 Mar 2006 13:39:55 +0000, in comp.lang.c , Ben Bacarisse
<be********@bsb.me.uk> wrote:
On Sun, 05 Mar 2006 11:58:54 +0000, Mark McIntyre wrote:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
No, the correct spelling would be matrices.

From Merriam-Webster:
ma·trix·es /'mA-trik-s&z/


We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


And no hint of irony!


If you didn't spot the irony in my post, you need a new irony
detector. It practially yelled "hey, I'm ironic". :-)

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 5 '06 #11
Mark McIntyre schrieb:
On Sun, 05 Mar 2006 13:39:55 +0000, in comp.lang.c , Ben Bacarisse
<be********@bsb.me.uk> wrote:
On Sun, 05 Mar 2006 11:58:54 +0000, Mark McIntyre wrote:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
>No, the correct spelling would be matrices.

From Merriam-Webster:

ma·trix·es /'mA-trik-s&z/

We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


And no hint of irony!


If you didn't spot the irony in my post, you need a new irony
detector. It practially yelled "hey, I'm ironic". :-)


Hmmm, it seemed to yell "spoiled by Latin lessons" at me ;-)
Somehow I must have missed your irony tags.

And yes: Matrixes is an abomination.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 5 '06 #12
Mark McIntyre wrote
(in article <6a********************************@4ax.com>):
On Sun, 5 Mar 2006 13:39:15 +0000 (UTC), in comp.lang.c , Richard
Heathfield <in*****@invalid.invalid> wrote:
Mark McIntyre said:
On 5 Mar 2006 00:24:50 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:

Micah Cowan wrote:

> No, the correct spelling would be matrices.

From Merriam-Webster:

ma·trix·es /'mA-trik-s&z/

We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


You'd better get on to Chambers, too, then. They list -ices and -ixes.


I never said which one was right :-)

(FWIW I prefer the european -ices over the atlantic -ixes, but the
latter is probably acceptable as long as one is consistent).


Perhaps I'm showing my age here, but I grew up on the "Atlantic"
side (which makes no sense at all btw, since the Atlantic
touches both), but it was always matrices for me. Still is. If
one of my kids comes home from school talking about matrixes
I'll inform them of their error. If the teacher does it, I'll
go have a talk with him/her as well.

In fact, I'm somewhat disappointed that my spell checker doesn't
flag matrixes as wrong.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 5 '06 #13
Micah Cowan <mi***@cowan.name> wrote:

No, the correct spelling would be matrices.


That depends on whether you're speaking Latin or English.

-Larry Jones

Moms and reason are like oil and water. -- Calvin
Mar 5 '06 #14
Mark McIntyre wrote:
<be********@bsb.me.uk> wrote:
On Sun, 05 Mar 2006 11:58:54 +0000, Mark McIntyre wrote:
<de***********@yahoo.com> wrote:
Micah Cowan wrote:

> No, the correct spelling would be matrices.

From Merriam-Webster:
ma·trix·es /'mA-trik-s&z/

We can't be held accountable for mistakes in Merriam-Webster's
dictionaries. I recommend writing to the authors to point it out.


And no hint of irony!


If you didn't spot the irony in my post, you need a new irony
detector. It practially yelled "hey, I'm ironic". :-)


Where's your pentameter? We need a count of pents in your iron.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 6 '06 #15
"Default User" <de***********@yahoo.com> writes:
Micah Cowan wrote:
my**************@gmail.com writes:
Hi,
I'm having some problems working with two-dimensional arrays. Here's
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.


No, the correct spelling would be matrices.


From Merriam-Webster:

Main Entry: ma·trix
Pronunciation: 'mA-triks
Function: noun
Inflected Form(s): plural ma·tri·ces /'mA-tr&-"sEz, 'ma-/; or
ma·trix·es /'mA-trik-s&z/


Huh. Well, I checked dict.org via the DICT protocol. It includes a
variety of dictionaries, including WordNet and The Collaborative
International Dictionary of English. It also includes Webster's
Revised Unabridged Dictionary (1913) [though that's apparently not in
the default search anymore]. None of them had matrixes. I can only
assume that it is a modern concession.

-Micah
Mar 6 '06 #16
Micah Cowan wrote:
Huh. Well, I checked dict.org via the DICT protocol. It includes a
variety of dictionaries, including WordNet and The Collaborative
International Dictionary of English. It also includes Webster's
Revised Unabridged Dictionary (1913) [though that's apparently not in
the default search anymore]. None of them had matrixes. I can only
assume that it is a modern concession.

Depends on what you mean by modern. It's listed in my American Heritage
(Second College) dictionary, copyright 1982.


Brian

Mar 6 '06 #17
On 6 Mar 2006 19:58:41 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.com> wrote:
Micah Cowan wrote:
None of them had matrixes. I can only
assume that it is a modern concession.

Depends on what you mean by modern. It's listed in my American Heritage
(Second College) dictionary, copyright 1982.


Modern begins around 1945 as far as dictionaries are concerned. Its
the opposite of history.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 6 '06 #18

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

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.