Reading byte array into float (expanding on SerialCallResponse example)

Just to clarify, you are still passing a value – the value is a pointer to an array (or an Object).

This matters because the pointer destination can be modified, but the receiving function can’t reassign things to the reference label that was used to pass that value, because it doesn’t have that reference – it only has the pointer value.

void setup() {
  int[] a = new int[]{0, 0};
  increase(a);  // yes -- can modify by passing value of array pointer
  println(a);   // {0, 1}
  replace(a);   // no -- cannot point 'a' at new contents, reference not passed
  println(a);   // {0, 1} -- not {9, 9}
}

void increase(int[] i) {  // succeeds, value is pointer
  i[1] = i[1]+1;
}

void replace(int[] i) {  // fails, not pass by reference so cannot modify the calling i
  i = new int[]{9, 9};
}

The classic example of this difference in Java is a simple function to “swap” two arrays or Objects provided as arguments, which fails – one example with detailed discussion is here:

2 Likes