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?
asked Jul 28, 2016 at 11:47
-
sizeof() is the number of bytes and not the number of elements. sizeof(array) / sizeof(element) gives the number of elements.Mikael Patel– Mikael Patel2016年07月28日 12:38:54 +00:00Commented Jul 28, 2016 at 12:38
1 Answer 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])
answered Jul 28, 2016 at 11:56
-
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" }; ThanksNgô Hữu Nam– Ngô Hữu Nam2016年11月14日 06:53:46 +00:00Commented Nov 14, 2016 at 6:53