Skip to Content
Circuitera

Pass an Arduino array and its element count to a helper

At the array declaration, sizeof(values) is the byte size of the whole array.

0 of 3 complete · Progress saved on this device

Work out the example

At the array declaration, sizeof(values) is the byte size of the whole array. Dividing by sizeof(values[0]) gives the element count. A typical Arduino function parameter declared const int values[] receives a pointer.

How many elements are in int values[] = {18, 22, 19, 23}?

Decide what to check next

An array helper should receive the count explicitly, or use an array reference where size inference is appropriate. Consider what an empty input should do and avoid dividing by zero.

Compare with the serial result

The sketch prints an element count of 4 and a sum of 82.

Open the exact Uno example

The sketch prints an element count of 4 and a sum of 82.

void setup() {
  Serial.begin(9600);
  const int values[]={18,22,19,23};
  const size_t count=sizeof(values)/sizeof(values[0]);
  int sum=0;
  for (size_t i=0; i<count; ++i) sum+=values[i];
  Serial.println(count);
  Serial.println(sum);
}
void loop() {}

Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.

Continue learning

Sources