Skip to content

Commit d139487

Browse files
committed
Improve sizeof example code
This improves the example code that demonstrates how to use sizeof to determine the number of elements in an array of any type. - In the description, specify that we're talking about arrays, just to be absolutely clear. - Define the array in the example code. - Add an explanatory comment to the for loop
1 parent ae039c9 commit d139487

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Language/Variables/Utilities/sizeof.adoc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ void loop() {
6666

6767
[float]
6868
=== Notes and Warnings
69-
Note that `sizeof` returns the total number of bytes. So for larger variable types such as ints, the for loop would look something like this.
69+
Note that `sizeof` returns the total number of bytes. So for arrays of larger variable types such as ints, the for loop would look something like this.
7070

7171
[source,arduino]
7272
----
73-
for (i = 0; i < (sizeof(myInts)/sizeof(myInts[0])); i++) {
74-
// do something with myInts[i]
73+
int myValues[] = {123, 456, 789};
74+
75+
// this for loop works correctly with an array of any type or size
76+
for (i = 0; i < (sizeof(myValues)/sizeof(myValues[0])); i++) {
77+
// do something with myValues[i]
7578
}
7679
----
7780

0 commit comments

Comments
 (0)