aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/arduino/sizeof.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/source/arduino/sizeof.rst')
-rw-r--r--docs/source/arduino/sizeof.rst69
1 files changed, 30 insertions, 39 deletions
diff --git a/docs/source/arduino/sizeof.rst b/docs/source/arduino/sizeof.rst
index a95dfbf..104c183 100644
--- a/docs/source/arduino/sizeof.rst
+++ b/docs/source/arduino/sizeof.rst
@@ -1,71 +1,62 @@
-.. _arduino-sizeof:
-
-sizeof
-======
-
-Description
------------
-
-The sizeof operator returns the number of bytes in a variable type,
-or the number of bytes occupied by an array.
+.. highlight:: cpp
+.. _arduino-sizeof:
+sizeof()
+========
Syntax
------
-sizeof(variable)
-
-
+::
-Parameters
-----------
+ sizeof(type)
+ sizeof(var)
-variable: any variable type or array (e.g. int, float, byte)
+Description
+-----------
-Example code
-------------
+The ``sizeof`` operator returns the number of bytes needed to store a
+value of a given type. This can be an ordinary numeric type, like
+``int``. It can be something more complicated, like a ``struct`` or
+``union``. If the argument to ``sizeof`` is an array, it returns the
+total number of bytes occupied by the array.
-The sizeof operator is useful for dealing with arrays (such as
-strings) where it is convenient to be able to change the size of
-the array without breaking other parts of the program.
+Examplec
+--------
+The ``sizeof`` operator is useful for dealing with arrays (such as
+strings) where it is convenient to be able to change the size of the
+array without breaking other parts of the program.
This program prints out a text string one character at a time. Try
-changing the text phrase.
-
-
-
-::
+changing the text phrase::
char myStr[] = "this is a test";
int i;
-
- void setup(){
+
+ void setup() {
Serial.begin(9600);
}
-
- void loop() {
- for (i = 0; i < sizeof(myStr) - 1; i++){
+
+ void loop() {
+ for (i = 0; i < sizeof(myStr) - 1; i++) {
Serial.print(i, DEC);
Serial.print(" = ");
Serial.println(myStr[i], BYTE);
}
}
-
-
-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.
-
-::
+Note that ``sizeof`` returns the total number of bytes; this is equal
+to the number of ``char``\ s only because the C++ standard guarantees
+that ``sizeof(char) == 1``. So for larger variable types such as
+``int``, the :ref:`for loop <arduino-for>` would look something like
+this::
for (i = 0; i < (sizeof(myInts)/sizeof(int)) - 1; i++) {
// do something with myInts[i]
}
-