April 19, 2024, 07:48:16 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How to assign elements of a two dimensional array?

Started by Haim, December 07, 2006, 12:26:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haim

Hi,
In the aurora tutorial there is an example of how to declare and assign a single dimensioned array of an intrinsic type (by a comma separated list of the values)

Is it possible to initialize and assign in this way arrays with 2 or 3 dimensions?
Is it possible to do that with arrays of simple structures (such as an array of POINT)?

This is not terribly important, but may save on a lot of typing...

Haim

Bruce Peaslee

December 07, 2006, 10:15:35 AM #1 Last Edit: December 07, 2006, 10:17:28 AM by peaslee
Try this:


sub main()
{
int myArray[3,3]; // 9 elements
int i;
int j;

myArray = 1,2,3,4,5,6,7,8,9;

openconsole();
for(j = 0; j < 3; j++)
{
for(i = 0; i < 3; i++)
{
print(myArray[i,j]);
}
}
while GetKey()=="";
closeconsole();
}
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Haim

Thanks,

I could not figure out though if this can be done with a structure such as POINT.

point pt[3];

can I assign like this: pt=......

Haim

Ionic Wind Support Team

The short answer is you can't.  However if all of the elements of a structure are the same size, and the struture size is a multiple of 4, there is a way to trick the compiler into doing it.


sub main()
{
POINT pt[10];
*(int)&pt = 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10;
for(int x=0;x < 10;x++)
{
print( pt[x].x,",",pt[x].y );
}
while GetKey()="";
}


This works with a point structure because it is comprized of two integer elements and the structure size is 8.

Paul.

Ionic Wind Support Team