I saw this code:
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
I know uint8_t
holds a byte, so how does this line of code work when a single
element, 0xFF
, is in itself one whole byte and the array is therefore 6 bytes wide.
Can someone explain how 6 bytes of data are of type single byte?
To try and probe it, I wrote this code on an Arduino Uno R3:
void setup() {
Serial.begin(115200);
// #include <stdint.h> // Not needed in Arduino IDE with Arduino board selected.
// #include <stdio.h> // Not needed in Arduino IDE with Arduino board selected.
}
void loop() {
delay(3000);
uint8_t numbers[] = {0xFF, 0x22, 0xA0, 0x3B, 0xC2}; // 5 elements & 5 bytes
Serial.print("sizeof(numbers) ");
Serial.println(sizeof(numbers)); // outputs = 5, (obviously the # elements in array)
for (int i = 0; i <= 8; i++) {
Serial.print("sizeof(numbers[");
Serial.print(i);
Serial.print("]: ");
Serial.println(sizeof(numbers[i])); // outputs 1 for i=0 to 8! Element value vanished! non-used elements positions equate to same as used element positions
}
}
saw this code: uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
I know uint8_t holds a byte, so how does this line of code work when a single element, 0xFF, is in itself one whole byte and the array is therefore 6 bytes wide.
broadcastAddress
is not a uint8_t
, but rather an array of uint8_t
s ([]
makes it an array).
It can therefore contain multiple elements of type uint8_t
.
The compiler will determine the actual size of the array based on the initializer ({0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
) - i.e. 6 in this case.
The same applies to numbers
in your test code (with 5 elements).