user70143: I'm trying to make a use of a relay module,
I want a clear way to configure and control my relay module
I've defined the struct and filled it with some info about id, pin number, title, and relay state
I can't loop with for in the array of controls, because I can't get the sizeof the array right
typedef struct{
  int id;
  int pin;
  String title;
  bool state;
}  SwitchControl;

SwitchControl controls[2] = {
  {1, 22, "Switch 01", 1},
  {2, 23, "Switch 02", 0},
};


for(int i=0; i<sizeof(controls) - 1; i++){
}

thanks

user10794: A more generic way (usable in future cases) is:
// number of items in an array
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

Now you can use ARRAY_SIZE (the_array) in your code like this:
for(int i=0; i<ARRAY_SIZE (controls); i++){
}

The above works in C as well as C++.

The more C++ way of doing it is to use a template like this:
// number of items in an array
template< typename T, size_t N > size_t ArraySize (T (&) [N]){ return N; }

Now ArraySize(the_array) returns the number of elements in the array:
for(int i=0; i< ArraySize (controls); i++){
}

The "Arduino language" is C++ with some initial pre-processing. For all intents and purposes any tutorial on C++ will help you with Arduino programming (except that the Standard Template Library (STL) is not included by default).

I don't know why you were subtracting 1 in your posted code. Did you not want to iterate through the whole array?

user58653: To make it generic, you simply divide the sizeof the array by the sizeof the type:
for(int i=0; i<sizeof(controls)/sizeof(SwitchControl); i++){
}

Then you can later change the items if the array, or the structure and the code will be the same.

70143: <p>I'm trying to make a use of a relay module,
I want a clear way to configure and control my relay module
I've defined the struct and filled it with some info about id, pin number, title, and relay state
I can't loop with for in the array of controls, because I can't get the sizeof the array right</p>
<pre><code>typedef struct{
  int id;
  int pin;
  String title;
  bool state;
}  SwitchControl;

SwitchControl controls[2] = {
  {1, 22, &quot;Switch 01&quot;, 1},
  {2, 23, &quot;Switch 02&quot;, 0},
};


for(int i=0; i&lt;sizeof(controls) - 1; i++){
}
</code></pre>
<p>thanks</p>
 10794: <p>A more generic way (usable in future cases) is:</p>
<pre class="lang-c++ prettyprint-override"><code>// number of items in an array
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
</code></pre>
<p>Now you can use ARRAY_SIZE (the_array) in your code like this:</p>
<pre><code>for(int i=0; i&lt;ARRAY_SIZE (controls); i++){
}
</code></pre>
<p>The above works in C as well as C++.</p>
<hr />
<p>The more C++ way of doing it is to use a template like this:</p>
<pre><code>// number of items in an array
template&lt; typename T, size_t N &gt; size_t ArraySize (T (&amp;) [N]){ return N; }
</code></pre>
<p>Now ArraySize(the_array) returns the number of elements in the array:</p>
<pre><code>for(int i=0; i&lt; ArraySize (controls); i++){
}
</code></pre>
<p>The &quot;Arduino language&quot; is C++ with some initial pre-processing. For all intents and purposes any tutorial on C++ will help you with Arduino programming (except that the Standard Template Library (STL) is not included by default).</p>
<hr />
<p>I don't know why you were subtracting 1 in your posted code. Did you not want to iterate through the whole array?</p>
 58653: <p>To make it generic, you simply divide the <code>sizeof</code> the array by the <code>sizeof</code> the type:</p>
<pre><code>for(int i=0; i&lt;sizeof(controls)/sizeof(SwitchControl); i++){
}
</code></pre>
<p>Then you can later change the items if the array, or the structure and the code will be the same.</p>
