I am using a function in Arduino C associated with writing data to a binary file on an SD-card. The prototype function is:
write(const uint8_t *buf, size_t len);
write accepts a pointer to a variable of type const uint8_t
, so if I am dealing with buffers containing elements of type uint16_t
I have to type cast them to use the write function. I tested this out with an Arduino:
uint8_t buffer8[10];
uint16_t buffer16[10];
void setup() {
Serial.begin(9600);
for(int i = 0; i<10; i++){
buffer8[i] = i;
buffer16[i] = i;
}
test(buffer8,sizeof(buffer8));
Serial.println();
test((const uint8_t *)buffer16, sizeof(buffer16)); //Without &
Serial.println();
test((const uint8_t *)&buffer16, sizeof(buffer16)); //With &
Serial.println();
}
void loop() {}
void test(const uint8_t *buf, size_t len){
for(int i = 0; i<len; i++){
Serial.print(buf[i]);
}
}
I got this output which is correct:
0123456789
00102030405060708090
00102030405060708090
When I call my test-function on buffer16
I do it two times. The first time I don't include &: test((const uint8_t *)buffer16, sizeof(buffer16));
. The second time I do include &: test((const uint8_t *)&buffer16, sizeof(buffer16));
.
But both function calls yield the same output. So my question is: Do I have to include the & when I type cast like this? What does this (const uint8_t *)&buffer16
actually mean in words?