473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

References to multidimensiona l array

I have a brain cramp and I need some help.

I have a chunk of code below which demonstrates
a problem I have with multidimensiona l arrays.
I want to keep it simple but something specific is getting in the way.

int a[10][10];
int b[10][10];
int **present;
int **next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
next[i][j]=present[i][j];
int **temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

Compare this to the one-dim version ( which works )
int a[10];
int b[10];
int *present;
int *next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
next[i]=present[i];
int *temp=next;
next=present;
present=temp;
test_for_done(p resent);
}


The reply-to email address is ol********@yaho o.com.
This is an address I ignore.
To reply via email, remove 2002 and change yahoo to
interaccess,

**
Thaddeus L. Olczyk, PhD

There is a difference between
*thinking* you know something,
and *knowing* you know something.
Jul 22 '05 #1
5 4492
TLOlczyk wrote:
I have a brain cramp and I need some help.

I have a chunk of code below which demonstrates
a problem I have with multidimensiona l arrays.
I want to keep it simple but something specific is getting in the way.

int a[10][10];
int b[10][10];
int **present;
int **next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
next[i][j]=present[i][j];
int **temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

Compare this to the one-dim version ( which works )
int a[10];
int b[10];
int *present;
int *next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
next[i]=present[i];
int *temp=next;
next=present;
present=temp;
test_for_done(p resent);
}


The code you posted doesn't seem to use 'a' or 'b' in either variation.

I suspect that the problem you have is due to the fact that an array
of more than one dimension cannot be converted to a pointer to pointer.
They are incompatible types.

Perhaps we can come back to this when you post the actual code with which
you have the problem.

Victor
Jul 22 '05 #2

"TLOlczyk" <ol********@yah oo.com> wrote in message
news:f8******** *************** *********@4ax.c om...
I have a brain cramp and I need some help.

I have a chunk of code below which demonstrates
a problem I have with multidimensiona l arrays.
I want to keep it simple but something specific is getting in the way.

int a[10][10];
int b[10][10];
int **present;
int **next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
next[i][j]=present[i][j];
At this point, you have undefined behavior, since neither next nor present
have been initialized to anything! (Did you mean to initialize them to
point to the start of a or b?)
int **temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

Compare this to the one-dim version ( which works )
int a[10];
int b[10];
int *present;
int *next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
next[i]=present[i];
Same problem here. THis causes undefined behavior.
int *temp=next;
next=present;
present=temp;
test_for_done(p resent);
}


Perhaps if you explained what you're trying to do?

-Howard
Jul 22 '05 #3
hi,

that is going to give compile errors most likely, and it *definitely*
won't work. The reason is the [] operator when used to create or index a
static array does something different than it does when being used with a
pointer variable.

Technically the reason is that no matter how many [] you use to define a
multidimensiona l array, the array is always a flat rendition with one row
contiguously following the last in memory. But if you declare a pointer
with multiple stars, what happens is that the pointer points to an array
of pointers, and each pointer in the indexed arrays points to another
array of pointers, and so on until you get to the last star where the
pointers each point to some data corresponding to rows given in the last
index.

The way it works out, if you have a single dimensional array the array
name is equivalent to a pointer to the array because you are already at
the last level of the pointers. But as you add dimensions to the array,
the layout grows different than the flat rendition as found in the static
version of the array, so your code won't work. If the compiler doesn't
tell you you can't make the assignment to temp, it is probably broken.

I'm drawing a blank as to how to fix it without resorting to something
like:

int *a[10]

and then allocating the elements for the other array index dynamically

David

TLOlczyk wrote:
I have a brain cramp and I need some help.

I have a chunk of code below which demonstrates
a problem I have with multidimensiona l arrays.
I want to keep it simple but something specific is getting in the way.

int a[10][10];
int b[10][10];
int **present;
int **next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
next[i][j]=present[i][j];
int **temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

Compare this to the one-dim version ( which works )
int a[10];
int b[10];
int *present;
int *next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
next[i]=present[i];
int *temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

The reply-to email address is ol********@yaho o.com.
This is an address I ignore.
To reply via email, remove 2002 and change yahoo to
interaccess,

**
Thaddeus L. Olczyk, PhD

There is a difference between
*thinking* you know something,
and *knowing* you know something.

Jul 22 '05 #4
int a[10][10];
int b[10][10];
int **present;
"present" points to the third ring of Saturn.
int **next;
"next" points to the Pluto's smaller moon.

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
next[i][j]=present[i][j];
So you're setting the contents of Saturn's third ring to the contents of
Pluto's smaller moon.
int **temp=next;
"temp" now points to Saturn's third ring.
next=present;
"next" now points to Pluto's smaller moon.
present=temp;
etc.
test_for_done(p resent);
}

Compare this to the one-dim version ( which works )
int a[10];
int b[10];
int *present;
int *next;

bool done=false;

while(!done)
{
// Loop which substitutes for complex calculation
for(int i=0;i<10;i++)
next[i]=present[i];
int *temp=next;
next=present;
present=temp;
test_for_done(p resent);
}

Why are you messing with these pointers?

Anyway, just for your information, here's how you define a reference to a
multidimensiona l array:

int a[10][10];
int b[10][10];

int (&x)[10][10] = a;
int (&y)[10][10] = b;
-JKop
Jul 22 '05 #5
TLOlczyk wrote:
I have a brain cramp and I need some help.

I have a chunk of code below which demonstrates
a problem I have with multidimensiona l arrays.
I want to keep it simple but something specific is getting in the way.


Other than declaring them, your code doesn't create use arrays
at all.

First thing you must realize. ARRAYS and POINTERS are not
the same thing. Further you can't even convert between
int array[10][10] and int**
they are not related types at all.

Second, let me further point out, that C++ really only has
single dimensioned arrays. A multidimension array array
is really an array of arrays.

int array[5][3]

array above is a 5 element array of 3 element arrays of int.

The implicit conversion of int[5][3] to a pointer is not int**,
it is int (*ptr)[3] (or in English: a pointer to a three element
array of type int). Every time ptr is incremented, it points at
the next 3 element array.
Chew over this and if you have further questions, try to be more
speicifc.
Jul 22 '05 #6

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

Similar topics

3
305
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is getting in the way. int a; int b; int **present; int **next;
9
6665
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
1
8254
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 RankException. But the MSDN documentation says: When copying between multidimensional arrays, the array
2
12825
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, comparing the text in Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
10
12199
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
1
9781
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to achieve an array that only has unique Product
5
2313
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30 =31 )
4
2506
Jezternz
by: Jezternz | last post by:
First of all I am open to any suggestions and advice. If a javscript multidimensional array is a bad way to do this please say so. I considered XML but I wondered if this would be a bad idea as it would be slower for jsp to handel. Okay here is an example of my multidimensional array (only part of it)(bottom of the post). It works fine I can call any of the 3rd dimension without trouble. Now How would I go about printing something like...
9
4494
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 function: x = new int;
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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
8639
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
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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...
1
2771
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.