problems with pointers

Hi All

Ive looked everywhere to try and figure out what Im doing wrong, but just cant get on top of this one. My sketch is using 4 functions for sending / receiving serial / IR + one function for translation between each of them. I have a bunch of arrays that I want the IR transmit function to use for timings, but I want to keep them as global constants, and have the translator send the IR transmit function the pointer to the array of timing ints. Ive tried what I thought were correct calls with & and * but cant make it work correctly.

Outside functions:

    const int IRvoldown[]={600,500,400,300,200,100};
    const int IRvolup[]={100,200,300,400,500,600};

In translator function:

if..... IRTX(IRvolup);
if..... IRTX(IRvoldown);
etc

IR TX function:

void IRTX(int IRTXcommand){
  Serial.println("IRTX: Received Data");
  Serial.print("IRTX: size of IRTXcommand is ");
  Serial.println(sizeof(IRTXcommand));
  Serial.print("IRTX: size of IRTXcommand[0] is ");
  Serial.println(sizeof(IRTXcommand[0]));
  for( int i=0;i < (sizeof(IRTXcommand)/sizeof(IRTXcommand[0]));i++){
  Serial.print("IRTX: i is ");
  Serial.print(i);
  Serial.print(". Value is ");
  Serial.println(IRTXcommand[i]);
  }
  Serial.println("IRTX: Just to prove the data is there: ");
  for( int i=0;i < 6;i++){
  Serial.print("IRTX: i is ");
  Serial.print(i);
  Serial.print(". Value is ");
  Serial.println(IRTXcommand[i]);
  }
}

Output is:
TRANSLATION: received SX, command 204500
IRTX: Received Data
IRTX: size of IRTXcommand is 2
IRTX: size of IRTXcommand[0] is 2
IRTX: i is 0. Value is 600
IRTX: Just to prove the data is there:
IRTX: i is 0. Value is 600
IRTX: i is 1. Value is 500
IRTX: i is 2. Value is 400
IRTX: i is 3. Value is 300
IRTX: i is 4. Value is 200
IRTX: i is 5. Value is 100

As you can see, the problem is that the sizeof() reports just the int size of 2, instead of proper length

If I put:

  • in like this: IRTX(*IRvolup);
    & in the function like this: void IRTX(int &IRTXcommand){
    I get errors:
    Full_System_just_working_on_IR_20110908.cpp: In function 'void translation(char, String)':
    Full_System_just_working_on_IR_20110908:74: error: invalid initialization of reference of type 'int&' from expression of type 'const int'
    Full_System_just_working_on_IR_20110908:8: error: in passing argument 1 of 'void IRTX(int&)'
    Full_System_just_working_on_IR_20110908:77: error: invalid initialization of reference of type 'int&' from expression of type 'const int'
    Full_System_just_working_on_IR_20110908:8: error: in passing argument 1 of 'void IRTX(int&)'
    Full_System_just_working_on_IR_20110908.cpp: In function 'void IRTX(int&)':
    Full_System_just_working_on_IR_20110908:93: error: invalid types 'int[int]' for array subscript
    Full_System_just_working_on_IR_20110908:94: error: invalid types 'int[int]' for array subscript
    Full_System_just_working_on_IR_20110908:98: error: invalid types 'int[int]' for array subscript
    Full_System_just_working_on_IR_20110908:105: error: invalid types 'int[int]' for array subscript

Thanka a lot!
regards
Andrew

Maybe this will help. When you pass an array name as a function parameter, you are passing a pointer to the first element of that array. In the function, if you write

int arrayName[32];
int s = sizeof(arrayName)

the compiler thinks you want the size of a pointer, which on the Arduino is 2 because a pointer is an int. In the function, if you were to dereference the array name and write

int arrayName[32];
int s = sizeof(*arrayName)

now the compiler thinks you want to know the size of what the first element of the array is which in you case is an int so again it's 2. Now if your array had instead been an array of uint8_t, that same statement would return 1 because that is the size of your array elements.
Outside the function, if you were to write

int arrayName[32];
int s = sizeof(arrayName);

the result would be 64, the number of bytes in your integer array.
Also, in your function, if you use the actual name of your global array, instead of the parameter name, which might refer to the same array, sizeof() will return the number of elements.

If you pass a pointer the size of the pointer will be 2 bytes.

I think your best option is to pass the size of the array along with the pointer to the array:

if..... IRTX(IRvolup, sizeof IRvolup/sizeof IRvolup[0]);
if..... IRTX(IRvoldown, sizeof IRvoldown/sizeof IRvoldown[0]);
etc

IR TX function:

void IRTX(int *IRTXcommand, unsigned size){
  Serial.println("IRTX: Received Data");
  Serial.print("IRTX: size of IRTXcommand is ");
  Serial.println(size);
  Serial.print("IRTX: size of IRTXcommand[0] is ");
  Serial.println(sizeof IRTXcommand[0]);
  for( int i=0; i < size; i++){
  Serial.print("IRTX: i is ");
  Serial.print(i);
  Serial.print(". Value is ");
  Serial.println(IRTXcommand[i]);
  }
  Serial.println("IRTX: Just to prove the data is there: ");
  for( int i=0;i < 6;i++){
  Serial.print("IRTX: i is ");
  Serial.print(i);
  Serial.print(". Value is ");
  Serial.println(IRTXcommand[i]);
  }

Thanks Both,

I had already tried passing the size also and had it working, but was not trusting myself on it!