Better way, perhaps using pointers?

Hi all.

This is what I've got. I know there is a better way, can you help suggest a tidy function?

int tiltMap[] = {
  380,640,01,99};  
int panMap[] = {
  380,640,99,01};

  void invertTiltInput() {
   int top = tiltMap[2];
   int bot = tiltMap[3];
   tiltMap[2] = bot;
   tiltMap[3] = top;
  }
  void invertPanInput() {
   int top = panMap[2];
   int bot = panMap[3];
   panMap[2] = bot;
   panMap[3] = top;
  }

There must be something i can do like :

invertInput(tiltMap);

Because I have a bunch of them, not just pan and tilt. They will be 'inverted' based on a LCD menu system to user preference..

Thoughts?

Cheers

M

pointers may only be confusing if you are not used to them.

Your arrays are essentially pointers that the compiler handles for you.

int tiltMap[] = {
  380,640,01,99};  
int panMap[] = {
  380,640,99,01};


  *panMap //is the same as 
  panMap[ 0 ]

  *( panMap + 3 ) //is the same as
  panMap[ 3 ]

Those functions are plenty tidy, very clear what they do.

A more generic version could be

  void invertStuff( int i_IndexA, int i_IndexB, int *i_Array )
    {
      int i_TempA = i_Array[ i_IndexA ], i_TempB = i_Array[ i_IndexB ];
      i_Array[ i_IndexA ] = i_TempB;
      i_Array[ i_IndexB ] = i_TempA;
      return; 
    }

  //Use like
  invertStuff( 2, 3, tiltMap );
  invertStuff( 2, 3, panMap );

http://devmaster.net/forums/topic/266-swapping-two-variables-without-using-a-temp-var/

Using pointers won't help you

void invertArray(int (& myArray) [4])
  {
int temp = myArray[2];
    myArray [2] = myArray[3];
    myArray [3] = temp; 
  }

int tiltMap[] = { 380,640,01,99};  
int panMap[] = { 380,640,99,01};

void setup ()
{
  invertArray (tiltMap);
  invertArray (panMap);
}
void loop () {}