Übrigens...
auto Funktionsparameter werden nicht von jedem Kompiler verarbeitet.
Darum hagelt es bei dir auch eine Warnung.
z.B. beim ESP geht es (noch) nicht.
// fuer moderne Kompiler C++11 und neuer
constexpr size_t arrayCellCount(const auto &array)
{
return sizeof(array)/sizeof(array[0]);
}
// ----
// fuer alte C++ Kompiler
#define ARRAYCELLCOUNT(array) (sizeof(array)/sizeof(array[0]))
// ----
// fuer mittelalte Kompiler (auch ESP)
template<typename DatenType> constexpr size_t ArrayCellCount(const DatenType &array)
{
return sizeof(array)/sizeof(array[0]);
}
// ---
void setup()
{
Serial.begin(9600);
int myInts[] = {1, 50, 500, 700};
Serial.println(arrayCellCount(myInts));
Serial.println(ArrayCellCount(myInts));
Serial.println(ARRAYCELLCOUNT(myInts));
}
void loop()
{
}
Allen 3 Varianten ist gemein, dass die Berechnung zur Kompilezeit erfolgt.
Der Array Type/Größe muss also auch schon zur Kompilezeit fest stehen.