Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an array to a function and using sizeof in Arduino

Tags:

c

arduino

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
}
like image 775
August Avatar asked Aug 18 '12 16:08

August


1 Answers

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.

like image 199
Some programmer dude Avatar answered Nov 10 '22 05:11

Some programmer dude