I'm an Arduino padawan learner. I want to pass a byte array to a function and get the size of the array.
byte data[] = {
B00000000,
B00000001,
B00000011,
B00000111
};
Serial.println(sizeof(data)); // ----------------------> displays 4
When I pass it to a function sizeof
returns 2. How can I get it to return 4 also in the function?
writeData(data);
void writeData(byte data[]) {
Serial.println(sizeof(data)); // -------------------> displays 2
}
When e.g. passing arrays as parameters, the compiler no longer has the size of the array. Instead the array gets passed as a pointer, and in writeData
the sizeof
operation gets the size of the pointer and not the array (or whatever) it points to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With