two-dimensional array

  • Thread starter my.correo.basura
  • Start date
M

my.correo.basura

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;
}
 
L

lawrence.jones

#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
 
J

jhartzell42

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
 
M

Micah Cowan

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.
 
K

Keith Thompson

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.
 
D

Default User

Micah said:
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
 
M

Mark McIntyre

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
 
R

Richard Heathfield

Mark McIntyre said:
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.
 
B

Ben Bacarisse

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). ;-)
 
M

Mark McIntyre

Mark McIntyre said:


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
 
M

Michael Mair

Mark said:
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
 
R

Randy Howard

Mark McIntyre wrote
(in article said:
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.
 
L

lawrence.jones

Micah Cowan said:
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
 
C

CBFalconer

Mark said:
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/>
 
M

Micah Cowan

Default User said:
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
 
D

Default User

Micah said:
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
 
M

Mark McIntyre

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
 

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,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top