array reference

I have a 2d array of 8, 8-byte addresses.

I want to pass one 8-byte address from the array to a function

Is there an efficient (less bytes code) way to do it than copying the bytes one-by-one to another array or passing the entire array to the function?

Like this?

const byte NUMBER_OF_ADRESSES = 8;
const byte ADRESS_BYTE_WIDTH = 8;

byte adresses[NUMBER_OF_ADRESSES][ADRESS_BYTE_WIDTH] = {
{ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 7 },
{ 11 , 12 , 13 , 14 , 15 , 16 , 17 , 17 },
{ 21 , 22 , 23 , 24 , 25 , 26 , 27 , 27 },
{ 31 , 32 , 33 , 34 , 35 , 36 , 37 , 37 },
{ 41 , 42 , 43 , 44 , 45 , 46 , 47 , 47 },
{ 51 , 52 , 53 , 54 , 55 , 56 , 57 , 57 },
{ 61 , 62 , 63 , 64 , 65 , 66 , 67 , 67 },
{ 71 , 72 , 73 , 74 , 75 , 76 , 77 , 77 }
};

void setup(){
Serial.begin(9600);
function( adresses[2] );//use 3rd adress
}

void loop(){

}

void function( byte* firstIndex ){
Serial.println(“Printing Adress:”);
for (byte i=0; i<ADRESS_BYTE_WIDTH; i++){
Serial.print(*(firstIndex++),DEC);
Serial.print(" ");
}
Serial.println();
}

:slight_smile:

[edit]Result:

Print Adress:

21 22 23 24 25 26 27 27

[/edit]

Works for me.

Man, this pointer stuff sure is black magic.

//pass the _adress_ of the 3rd adress array
function( adresses[2] );

void function( byte* firstIndex ){
  for (byte i=0; i<ADRESS_BYTE_WIDTH; i++){
    byte value = *(firstIndex++);
  }
}

demystify *(firstIndex++)

Now. Becase firstIndex is actually pointing to a byte already initialized in memory, we’ll need to use * to get the value.
(By not using the * we would’ve operated on the adress and not the value value.)

byte variable = 0;
variable++;
//variable is 1 after the ++

With the firstIndex++ we’ll get something like this:

byte variable = 0;
byte* firstIndex = &variable; //assign the adress of variable to firstIndex, say 120 for simplicity
firstIndex++; //as with variable++ increase the value by one, but as the value of finstIndex is an adress, increase the adress by one
//firstIndex is 121 after the ++

The last part of the black magic, is that in an array, all datas are stored sequentionally in RAM.
So, one can presume that

byte array[2] = { 100 , 200 };

array[0] will be in memory exactly one andress before array[1]

This is why the firstIndex++ will work. :slight_smile:

You can write

for (byte i=0; i<ADRESS_BYTE_WIDTH; i++){
    byte value = *(firstIndex++);
  }

like this:

for (byte i=0; i<ADRESS_BYTE_WIDTH; i++){
    byte value = firstIndex[i];
  }