
passing of pointers to int arrays?
I'm doing a little submarine game, with a basic 3x2 array 'pos' to represent
the BOW, MID & STERN X & Y positions in an arena.
pos[3][2] is declared in a general function (not main), which calls many
other specific functions to manipulate specific game variables. I have a
function setpos() which given the position of the BOW block & the current
heading (North, South, East, West), should be able to calculate the MIDSHIP
& STERN positions.
It didn't take me long into the coding to realise that if I'm to manipulate
the original array integers, I need to pass it as pointers. then it hit me
: pointers to int I've done before, but pointers to int arrays.... heck.
given my understand of char arrays, the name of the variable is the location
of the 1st element, so the name will naturally point to the 1st int element
as well, right? so this is what I wrote :
void setpos(int *pos, int heading)
{
switch (heading)
{
case NORTH :
*(pos[MID][X]) = *(pos[STERN][X]) = *(pos[BOW][X]);
*(pos[MID][Y]) = *(pos[BOW][Y]) + 1;
*(pos[STERN][Y]) = *(pos[MID][Y]) + 1;
break;
case SOUTH :
pos[MID][X] = pos[STERN][X] = pos[BOW][X];
pos[MID][Y] = pos[BOW][Y] - 1;
pos[STERN][Y] = pos[MID][Y] - 1;
break;
case EAST :
pos[MID][Y] = pos[STERN][Y] = pos[BOW][Y];
pos[MID][X] = pos[BOW] - 1;
pos[STERN][X] = pos[MID][X] - 1;
break;
case WEST :
pos[MID][Y] = pos[STERN][Y] = pos[BOW][Y];
pos[MID][X] = pos[BOW] + 1;
pos[STERN][X] = pos[MID][X] + 1;
break;
}
Quote:
}
The above code shows my 2 futile attempts at 2 different dereferencing
styles - both compiled with error "subscripted value is neither array nor
pointer". heck.... my understanding sucks. how do I manipulate an external
array?
:(
Aaron