user61953: Printing structure variables, I get default values. What is wrong with my code?
struct SCENARIO
{
  int Lamp_Pin = -1;
  int PB_Pin = -1;
} ;

SCENARIO  _red;
SCENARIO  _yellow;
SCENARIO  _white;
SCENARIO  _stop;
SCENARIO btns[4] = { _red,  _yellow, _white, _stop};


void setup()
{ 
  _stop.Lamp_Pin = 8;
  _stop.PB_Pin = 4;

 ///// initializing other 3 objects i.e. red & white & yellow
 //// 
for (int i = 0; i < 4; i++)
  {
    Serial.print(i);Serial.print(":");
    Serial.print(btns[i].Lamp_Pin);Serial.print(":");
    Serial.println( btns[i].PB_Pin );
    //pinMode(  btns[i].Lamp_Pin, OUTPUT);
    //pinMode(  btns[i].PB_Pin, INPUT);
  }
}

The image below shows what I get. It seems I have not initialized the variables!


user31105: The problem is that a struct is handled as a value type, not as a reference type.
This means that a copy of _stop is made and put in btns (btns[3] actually).
So what you need to do is putting pointers into the btns array:
SCENARIO* btns[4] = { &_red, &_yellow, &_white, &_stop};

The symbol * denotes a pointer, and & means the address of a variable (so it points to the variable and is not a copy).
Now when you change _stop, also btns[3] which points to _stop will show the correct initialized value.
For printing you should use:
Serial.println( (*btns[i]).LampPin);Serial.print(":");
Serial.println( (*btns[i]).PB_Pin );

Because *btns[i] is a pointer, but because this is a very often used feature, a special notation can be used:
Serial.print(btns[i]->Lamp_Pin);Serial.print(":");
Serial.println( btns[i]->PB_Pin);


user4143: In this line:
SCENARIO btns[4] = { _red,  _yellow, _white, _stop};

you are making 4 copies of the structures and placing them in an array.
Instead you should either create the array directly with the values:
SCENARIO btns[4] = {
    {-1, -1},
    {-1, -1},
    {-1, -1},
    {8, 4}
};

And only use the array, or use pointers in the array:
SCENARIO *btns[4] = { &_red,  &_yellow, &_white, &_stop };

and then access the entries as pointers:
Serial.print(btns[i]->Lamp_Pin);
Serial.print(":");
Serial.println( btns[i]->PB_Pin );


61953: <p>Printing structure variables, I get default values. What is wrong with my code?</p>

<pre><code>struct SCENARIO
{
  int Lamp_Pin = -1;
  int PB_Pin = -1;
} ;

SCENARIO  _red;
SCENARIO  _yellow;
SCENARIO  _white;
SCENARIO  _stop;
SCENARIO btns[4] = { _red,  _yellow, _white, _stop};


void setup()
{ 
  _stop.Lamp_Pin = 8;
  _stop.PB_Pin = 4;

 ///// initializing other 3 objects i.e. red &amp; white &amp; yellow
 //// 
for (int i = 0; i &lt; 4; i++)
  {
    Serial.print(i);Serial.print(":");
    Serial.print(btns[i].Lamp_Pin);Serial.print(":");
    Serial.println( btns[i].PB_Pin );
    //pinMode(  btns[i].Lamp_Pin, OUTPUT);
    //pinMode(  btns[i].PB_Pin, INPUT);
  }
}
</code></pre>

<p>The image below shows what I get. It seems I have not initialized the variables!</p>

<p><a href="https://i.stack.imgur.com/SOoKi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SOoKi.png" alt="enter image description here"></a></p>
 31105: <p>The problem is that a struct is handled as a value type, not as a reference type.</p>

<p>This means that a copy of <code>_stop</code> is made and put in <code>btns</code> (<code>btns[3]</code> actually).</p>

<p>So what you need to do is putting pointers into the <code>btns</code> array:</p>

<pre><code>SCENARIO* btns[4] = { &amp;_red, &amp;_yellow, &amp;_white, &amp;_stop};
</code></pre>

<p>The symbol * denotes a pointer, and &amp; means the address of a variable (so it points to the variable and is not a copy).</p>

<p>Now when you change <code>_stop</code>, also <code>btns[3]</code> which points to <code>_stop</code> will show the correct initialized value.</p>

<p>For printing you should use:</p>

<pre><code>Serial.println( (*btns[i]).LampPin);Serial.print(":");
Serial.println( (*btns[i]).PB_Pin );
</code></pre>

<p>Because *btns[i] is a pointer, but because this is a very often used feature, a special notation can be used:</p>

<pre><code>Serial.print(btns[i]-&gt;Lamp_Pin);Serial.print(":");
Serial.println( btns[i]-&gt;PB_Pin);
</code></pre>
 4143: <p>In this line:</p>

<pre><code>SCENARIO btns[4] = { _red,  _yellow, _white, _stop};
</code></pre>

<p>you are making 4 copies of the structures and placing them in an array.</p>

<p>Instead you should either create the array directly with the values:</p>

<pre><code>SCENARIO btns[4] = {
    {-1, -1},
    {-1, -1},
    {-1, -1},
    {8, 4}
};
</code></pre>

<p>And only use the array, or use pointers in the array:</p>

<pre><code>SCENARIO *btns[4] = { &amp;_red,  &amp;_yellow, &amp;_white, &amp;_stop };
</code></pre>

<p>and then access the entries as pointers:</p>

<pre><code>Serial.print(btns[i]-&gt;Lamp_Pin);
Serial.print(":");
Serial.println( btns[i]-&gt;PB_Pin );
</code></pre>
