address of array

Hallo,
I want to get the address of array 'arr[]' and store its low and high part to dbgarr[2]. Is there a way how to do it?
IDE returns compile time error on macro highByte(), lowByte() on example below...
thx.

// file: print_address_test.ino
// how can I extract the address of array ?
//
const char arr[]= {'0','1','2','3'};
uint8_t dbgarr[2];

void setup() {
  dbgarr[0] = highByte(&arr[0]); // <--fails
  dbgarr[1] = lowByte(&arr[0]); // <--fails  
}

void loop() {
}
// eof
In file included from print_address_test.ino:4:0:
print_address_test.ino: In function 'void setup()':
F:\programs_x86\arduino-1.6.5\hardware\arduino\avr\cores\arduino/Arduino.h:100:38: error: invalid operands of types 'const char*' and 'int' to binary 'operator&'
 #define lowByte(w) ((uint8_t) ((w) & 0xff))

It would be more helpful if you explained why you want to get the address of the array and what you want to do with it.

frpr666:
I want to get the address of array 'arr[]' and store its low and high part to dbgarr[2]. Is there a way how to do it?

The address of the array 'arr[]' is stored in the name 'arr' itself.

Addresses in 8-bit controllers are 2-byte values, so you can easily typecast them to an 'int':

// file: print_address_test.ino
// how can I extract the address of array ?
//
const char arr[]= {'0','1','2','3'};
const char brr[]= {'4','5','6','7'};
int ptr;

void setup() {
  Serial.begin(9600);
  ptr=(int)arr;
  Serial.println(ptr);
  ptr=(int)brr;
  Serial.println(ptr);
}

void loop() {
}

Another way is:

// file: print_address_test.ino
// how can I extract the address of array ?
//
const char arr[]= {'0','1','2','3'};
uint8_t dbgarr[2];

union {
  uint8_t dbgarr[2];
  unsigned val;
} myUnion;

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

  myUnion.val = (unsigned) &arr;
  Serial.println((unsigned) &arr);
  Serial.print("addr[0] = ");
  Serial.print(myUnion.dbgarr[0], HEX);
  Serial.print("   addr[1] = ");
  Serial.print(myUnion.dbgarr[1], HEX);
}

void loop() {
}

This also helps you to see the endian issues for memory addresses.

I'm using some pointer aritmetics in my sketch and would like to debug stuff like ptr++.
It seems the Serial.println(ptr) is the solution.
Thx

int ptr;

Why would anyone name a variable that is not a pointer "ptr"?

PaulS:

int ptr;

Why would anyone name a variable that is not a pointer "ptr"?

Because its the representation of a pointer cast to an int?

  dbgarr[0] = highByte((int) &arr[0]); // cast pointer to int
  dbgarr[1] = lowByte((int) &arr[0]); // cast pointer to int

PaulS:

int ptr;

Why would anyone name a variable that is not a pointer "ptr"?

Just as a reminder, I suppose.

Same thing as with variable names like "led", "button", "relay" and so on: People choose mnemonic names while they know that a variable named "led", "button", "relay" are not a real led, button or relay. They just represent what the name stands for. Same with an int named "ptr".

I have problem the same as you.
What did u do?