473,668 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=matr ix1; /*Line 15*/
dummyMatrix=mat rix2; /*Line 16*/

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

return 0;
}

Mar 4 '06 #1
17 2189
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=matr ix1; /*Line 15*/
dummyMatrix=mat rix2; /*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=matr ix1; /*Line 15*/
dummyMatrix=mat rix2; /*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=real Matrix;
realMatrix=dumm yMatrix;
dummyMatrix=swa pMatrix;
}

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=matr ix1; /*Line 15*/


Arrays are not pointers, and pointers-to-pointers are not useful in
dealing with true 2-dimensional arrays. (Multidimension al 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_Keit h) 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*****@invali d.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

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

Similar topics

0
1780
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. Inside (or just after) the same query that searches for available seats, I need to SIMULTANEOUSLY mark those seats as "on hold". I've only read about, but not yet used MySQL transactions, and wonder if this simultaneous "search-and-hold"...
8
1742
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). TABLE matchResults cont_id team_id contest_result 1 1 3 1 2 5
6
1871
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
9346
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 record in the second table and call the update method of the data adapter the command builders update command text is for the first table. Can the command builder handle two tables? Code example: Dim oCOnn As New SqlConnection("Data Source=.;" &...
6
4072
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 the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
7
12883
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 compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function or simple code snippet.. Thank U -- Peter...
0
1684
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 became realllllllllllly slow. Content in LoginView Role Groups was not displaying even after a user in a role had logged in. It was taking about 15 seconds or so for the login control to display when the login link was selected on the homepage....
9
5259
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 w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
9
2015
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 million times, but I can't seem to find the answer. For example, I have two classes stored in separate files as such. File: one.py ======== class One:
0
8462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8381
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.