# Where is the correct place to create a variable with has a defined structure

**URL:** <https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440>\
**Category:** Programming\
**Created:** [April 17, 2023, 8:01pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440 "2023-04-17T20:01:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerio414](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rogerio414](https://forum.arduino.cc/u/rogerio414)\
**Post date:** [April 17, 2023, 8:01pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/1 "2023-04-17T20:01:41Z")

</div>

Dear all,  
I have no training in C++ and like, and I my knowledge comes from few books and a lot of studying other people codes.  
It seems that every time one creates a structure for a variable, that structure is defined before the setup instruction.  
However the place where the variable using that structure is created seems to depend on the programmer.  
I have seen it defined in the setup section and in the loop section.  
My question is: what are the benefits (if any) of placing this command in either place.  
Do I need to place in the loop so the values are updated?  
Thanks

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 17, 2023, 8:04pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/2 "2023-04-17T20:04:48Z")

</div>

It all depends on the desired **[scope](https://forum.arduino.cc/t/what-is-scope/369179)** of the variable or structure (struct).

A variable declared "before setup()" or **outside** of any function is visible to the whole program. Global scope.

A variable declared **inside** setup() or any function is only visible to that function. Local scope.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [April 17, 2023, 8:04pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/3 "2023-04-17T20:04:57Z")

</div>

Are you referring to a **struct** or a simple variable like **byte myVariable**

Google **local/global** variable.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [April 17, 2023, 8:07pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/4 "2023-04-17T20:07:43Z")

</div>

For simple microcontroller code you can safely declare all variables at the top before setup. As you learn more you will want to limit the scope of variables by declaring them inside the functions they are used in.

---

<div class="post-metadata">

**Author:** ![apf1979](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/apf1979/32/966781_2.png) [@apf1979](https://forum.arduino.cc/u/apf1979)\
**Post date:** [April 17, 2023, 8:16pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/5 "2023-04-17T20:16:26Z")

</div>

> [@PerryBebbington](#):
>
> you will want to limit the scope of variables by declaring them inside the functions they are used in.

I believe this was how the Fiat Chrysler vehicles Bluetooth hack worked. A global variable wrote to functions they shouldn't have and allowed the Bluetooth connection to control other stuff like engine, brakes, and steering.

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 17, 2023, 8:23pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/6 "2023-04-17T20:23:32Z")

</div>

> [@apf1979](#):
>
> the Fiat Chrysler vehicles Bluetooth hack

I'm puzzled by the use of the definite article in that phrase, like it's a well-known thing.

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [April 17, 2023, 8:26pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/7 "2023-04-17T20:26:35Z")

</div>

Another benefit is that local variables "come to life" when control enters the function where they are defined, and then "die" when control leaves that function. Global variables are eating up data space all the time, perhaps even when they are not really needed.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 17, 2023, 8:27pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/8 "2023-04-17T20:27:55Z")

</div>

Having both local and global variables with the same name is a common new user mistake. We would have caught that if Fiat Chrysler had run their code past us. 🙂 (removing tongue from cheek).

---

<div class="post-metadata">

**Author:** ![apf1979](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/apf1979/32/966781_2.png) [@apf1979](https://forum.arduino.cc/u/apf1979)\
**Post date:** [April 17, 2023, 8:27pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/9 "2023-04-17T20:27:58Z")

</div>

Well fortunately the likelihood of people visiting this post also having access to a search engine is pretty high. And I never said it was well known. It's the most well known hack of Bluetooth on Chrysler vehicles though.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 17, 2023, 8:30pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/10 "2023-04-17T20:30:05Z")

</div>

Then there is the static keyword that effects the "lifetime" of a local variable. A static local variable does not die when the function is done.

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 17, 2023, 8:30pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/11 "2023-04-17T20:30:34Z")

</div>

Chrysler's not that big a deal in this part of the world.  
I thought maybe this was something new, but it looks like pretty old news.

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [April 17, 2023, 8:46pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/12 "2023-04-17T20:46:56Z")

</div>

groundFungus; that's true. A _static_ variable defined within a function has its scope restricted to that function, but it retains its value between calls to the function. A _static_ variable defined outside a function has scope from its point of definition to the end of the file in which it is defined. This can be very useful when a project is comprised of multiple source code files.

---

<div class="post-metadata">

**Author:** ![rogerio414](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rogerio414](https://forum.arduino.cc/u/rogerio414)\
**Post date:** [April 18, 2023, 8:05pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/13 "2023-04-18T20:05:32Z")

</div>

Thank you very much for the replies.  
I think I understand it better now, although my primary concern about assigning a struct to a variable in the loop section was "waste of computing power".

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 15, 2023, 8:06pm UTC](https://forum.arduino.cc/t/where-is-the-correct-place-to-create-a-variable-with-has-a-defined-structure/1116440/14 "2023-10-15T20:06:34Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
