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?

share|improve this question
    
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
up vote 1 down vote accepted

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])
share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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