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?

1
  • sizeof() is the number of bytes and not the number of elements. sizeof(array) / sizeof(element) gives the number of elements. Commented Jul 28, 2016 at 12:38

1 Answer 1

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])
1
  • 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 Commented Nov 14, 2016 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.