Passing an array to a function and using sizeof in Arduino

arduino, c

Solution

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.

Problem

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 } ```

Original source