# Is possible to Initialize a variable with an structure type

**URL:** <https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128>\
**Category:** Programming\
**Created:** [October 27, 2015, 6:39pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128 "2015-10-27T18:39:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pabusa](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pabusa](https://forum.arduino.cc/u/pabusa)\
**Post date:** [October 27, 2015, 6:39pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/1 "2015-10-27T18:39:09Z")

</div>

I mean. Imagine I have:

_typedef struct_  
_{ int posx;_

- int posy;\*
- int ancho;\*
- int alto;\*
- String etiqueta;\*  
_} botones2;_

and I define a var

_botones2 miboton;_

Can I do something like this:

_miboton = (30,15, 20, 40, "My Label")_

...or do I have to assign values field by field??

---

<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 27, 2015, 6:44pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/2 "2015-10-27T18:44:19Z")

</div>

> Can I do something like this:

Using curly braces instead of parentheses, yes, IF you do it as part of the declaration:

```arduino
botones2 miboton = {30,15, 20, 40, "My Label"};

```

---

<div class="post-metadata">

**Author:** ![pabusa](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pabusa](https://forum.arduino.cc/u/pabusa)\
**Post date:** [October 27, 2015, 8:01pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/3 "2015-10-27T20:01:11Z")

</div>

Ok, thank very much... I tell you what I am looking for....

I have initialized the buttons as you told me:

```cpp
botones boton1 = {30,15, 20, 40, "boton1"};
botones boton2 = {30,15, 20, 40, "boton2"};
botones boton3 = {30,15, 20, 40, "boton3"};
botones boton4 = {30,15, 20, 40, "boton4"};

```

And now I have, a second struct (and a third struct) like that:

```cpp
typedef struct 
{
  String nzona;
  botones boton[];
} zonas;

typedef struct 
{
  String nzona;
  botones boton[];
} zonas;

typedef struct 
{
  String nsitio; //The name of the site
  zonas zona[5];
} sitio;

sitio misitio;

```

I want to reference like that... (in fact I am not sure if I have defined the strucs right)

misitio.zona[0].boton[0].posx = 30;

or

misitio.zona[1].nzona = "Texto Zona 1";

and things like that...... but I have 2 main doubts.

1 - Am I defining the structures right? I don't know, but it is like I see something strange.

2 - I would like to fill the structure in the time I declare... but I am not able, I loose myself. Any advise about it? I just "fills" buttons as you told me, but I loose again to zonas and site....

Thank you very mucha.

---

<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 27, 2015, 10:13pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/4 "2015-10-27T22:13:18Z")

</div>

> 1 - Am I defining the structures right? I don't know, but it is like I see something strange.

No. When you leave out a value in the braces to define the size of an array, the compiler counts the initializers. You have none, so you will get a zero sized array. Not very useful.

Also, dump the String class before you run out of memory.

> 2 - I would like to fill the structure in the time I declare... but I am not able, I loose myself.

You should be using arrays of pointers. After defining the botones instances, you can use the instances to populate the array, when you size it properly, with the addresses of the instances.

---

<div class="post-metadata">

**Author:** ![pabusa](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pabusa](https://forum.arduino.cc/u/pabusa)\
**Post date:** [October 28, 2015, 2:02pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/5 "2015-10-28T14:02:29Z")

</div>

> PaulS:  
> No. When you leave out a value in the braces to define the size of an array, the compiler counts the initializers. You have none, so you will get a zero sized array. Not very useful.
> 
> Also, dump the String class before you run out of memory.  
> You should be using arrays of pointers. After defining the botones instances, you can use the instances to populate the array, when you size it properly, with the addresses of the instances.

I don¡t mind to fix it .... I am new on it and by now I am avoiding pointers.

Ummm.... so the problem is that I don't write the dimension of the arrays... If I did would it work the struct?

---

<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:** [May 5, 2021, 9:32pm UTC](https://forum.arduino.cc/t/is-possible-to-initialize-a-variable-with-an-structure-type/343128/7 "2021-05-05T21:32:38Z")

</div>

Closed on May 5, 2021, 9:32pm UTC


