Returning the address of an array in memory

Hello,

why can't I persuade the Arduino to return the address of an array in memory?

Here's my code:

bool bDo = true;
char arrString[12] = {'T','e','s','t',' ','S','t','r','i','n','g','\0'};

void setup() {
  Serial.begin(9600);
}

void loop() {
  while(bDo){
    Serial.write("Array Data = ");
    Serial.write(arrString);
    Serial.write(10);
    Serial.write("Array Address = ");
    Serial.write(&arrString[0]);
    
    bDo = false;
  }
}

Bafflingly, Serial.write(arrString); returns exactly the same result as Serial.write(&arrString[0]);...surely, it should return the address of the first element of the array?

I'm sure I've done this in c (years ago) and it worked then - is it something to do with .write trying to be too clever for its own good?

Any suggestions would be appreciated,

Thanks :slight_smile:

Bafflingly, … returns exactly the same result as … surely, it should return the address of the first element of the array?

Why are you using Serial.write() to send data to the Serial Monitor? What you you expect the BINARY value being sent to mean to an application that expects ASCII data?

Both of those expressions are character pointers (the address of a character in memory). If you want to print the value of the pointer you should cast it as an unsigned integer. If you use .write() on an integer you will get a single character. To display an integer, use .print(). Addresses are traditionally displayed as hexadecimal.

    Serial.write("Array Address = ");
    Serial.print((unsigned)(&arrString[0]), HEX);

0x10c:
Bafflingly, Serial.write(arrString); returns exactly the same result as Serial.write(&arrString[0]);...surely, it should return the address of the first element of the array?

It's function overloading that works this magic. Serial.write() "knows" the data type of what you send to it.

Thanks chaps!

I thought .print was causing my formatting problems, so I went for .write to try to get around exactly this problem...

...my array is at address 0x100 apparently...in case you couldn't live without knowing :slight_smile:

Thanks :slight_smile: