passing of pointers to int arrays?
Author Message 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 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 :

{
{
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

Fri, 08 Mar 2002 03:00:00 GMT  passing of pointers to int arrays?
Here is an example that passes arrays:

#include <iostream>
#include <iomanip>

{
for(int i = 0;i < 12;i++) {
for(int j = 0;j < 12;j++) {
array[i][j] = (i+1) * (j+1);
}
}

Quote:
}

void showArray(int array[])
{
for(int i = 0;i < 12;i++) {
for(int j = 0;j < 12;j++) {
cout << setw(4) << array[i][j];
}
cout << endl;
}

Quote:
}

int main()
{
int (*array) = new int;
showArray(array);
delete [] array;
return 0;

Quote:
}

--

Paul Lutus
www.arachnoid.com

<snip>

Fri, 08 Mar 2002 03:00:00 GMT  passing of pointers to int arrays?
Sorry about the C++! The post to which I responded was cross-posted in a C++
NG.

--

Paul Lutus
www.arachnoid.com

Quote:
> Here is an example that passes arrays:

> #include <iostream>
> #include <iomanip>

> {
>  for(int i = 0;i < 12;i++) {
>   for(int j = 0;j < 12;j++) {
>    array[i][j] = (i+1) * (j+1);
>   }
>  }
> }

> void showArray(int array[])
> {
>  for(int i = 0;i < 12;i++) {
>   for(int j = 0;j < 12;j++) {
>    cout << setw(4) << array[i][j];
>   }
>   cout << endl;
>  }
> }

> int main()
> {
>  int (*array) = new int;
>  showArray(array);
>  delete [] array;
>  return 0;
> }

> --

> Paul Lutus
> www.arachnoid.com

> <snip>

Fri, 08 Mar 2002 03:00:00 GMT  passing of pointers to int arrays?
Yes, the int pos[] syntax works very well.
so it turns out pos' a dbl pointer & pos, pos, etc are single
array pointers.

Thanx!

Sat, 09 Mar 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages