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



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>

void loadArray(int array[][12])
{
 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[][12])
{
 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)[12] = new int[12][12];
 loadArray(array);
 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>

> void loadArray(int array[][12])
> {
>  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[][12])
> {
>  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)[12] = new int[12][12];
>  loadArray(array);
>  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[][2] syntax works very well.
so it turns out pos' a dbl pointer & pos[0], pos[1], etc are single
array pointers.

Thanx!



Sat, 09 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. int pointer to int array

2. Passing pointers to arrays of pointers....

3. how do I pass int array by reference?

4. Passing an int array through argv WAS: my own Very revised aueston

5. Passing struct to dll with int array

6. how to modify a pointer to an array one sizeof(int)

7. Sorting array of pointers to int

8. pointer to int array question

9. Returning Int array pointers from functions

10. initializing array of pointers to int

11. Passing Arrays of Pointers and malloc

12. pass a pointer to a multiple dimension array

 

 
Powered by phpBB® Forum Software