Page 1 of 1

PostPosted: Mon May 23, 2005 2:22 am
by Zaine
ok i know this topic has been on these forums, but can somebody pls explain to me how does the pointer arimethic in PocketC language file work? i'm really struggling to learn how to work stuff in pocketC, and the forum threads are all beyond me =P i tried the following code

int array[100];
pointer twod[10]; // after init(), this can be treated
// like at 10x10 matrix

init() {
int i;
for (i=0;i<10;i++)
twod[i]=array + i*10; // Pointer arithmetic
}

main() {
int x, y;
init();
for (x=0;x<10;x++)
for (y=0;y<10;y++)
twod[x][y]=x * y; // Sets array[x*10 + y] = x*y
}




but it just gave me a Virtual Machine Error
addr: 83, Func:init, Global ref out of range

then i tried this method by FrankG

int xtable[100]; // but I want 25x4

int ix(int row, int col) {return row*25+col;}

for (row=0;row<25;row++)
for (col=0;col<25;col++)
xtable[ix(row,col)] = 0;

tho i couldn't get it to work

any help would be appreciated, thanks !

PostPosted: Tue May 24, 2005 7:25 pm
by Vilmos
Code: Select all
int xtable[100]; // but I want 25x4

int ix(int row, int col) {return row*25+col;}

for (row=0;row<25;row++)
for (col=0;col<25;col++)
xtable[ix(row,col)] = 0;


If you want 25x4 then you need to change one of your for loops to 4 instead of 25! If you really want 25x25 then your initial array needs to be 625 instead of 100.

Should work other than that.

Vilmos
http://www.pocketdimension.com

PostPosted: Fri May 27, 2005 3:21 am
by Zaine
:D thanks! i got it working.

i sure need to work more on my programming ...

PostPosted: Fri May 27, 2005 10:02 am
by JohnM
The best way to learn is by doing it :)

- Bergman Software