0

I am trying to find out the size of an array like this:

    String days[3] = { "Mon", "Tue", "Wed" };
Serial.printf("Size of array: %2d\n", sizeof(days));
for (int i = 0; i < sizeof(days); i++) {
    Serial.print(days[i]);
}

The result of the code above is 36 which is incorrect.

Do I have any other options considering that I don't know the size of the array to begin with?

  • sizeof() is the number of bytes and not the number of elements. sizeof(array) / sizeof(element) gives the number of elements. – Mikael Patel Jul 28 '16 at 12:38
1

No, the result is correct, you're just interpreting it incorrectly. If you want the number of elements in a statically-allocated array use:

sizeof(somearray) / sizeof(somearray[0])
| improve this answer | |
  • Sorry if I post in wrong topic follow answer of Ignacio Vazquez-Abrams, in case of size of each String in array is different? like String days[] = { "Monday", "Tuesday", "Wednesday" }; Thanks – Ngô Hữu Nam Nov 14 '16 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.