# Struct containing arrays

**URL:** <https://forum.arduino.cc/t/struct-containing-arrays/974937>\
**Category:** Programming\
**Created:** [March 29, 2022, 6:03pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937 "2022-03-29T18:03:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Ward](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dan_ward/32/1058773_2.png) [@Dan\_Ward](https://forum.arduino.cc/u/Dan_Ward)\
**Post date:** [March 29, 2022, 6:03pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/1 "2022-03-29T18:03:18Z")

</div>

Hi, With an Arduino Uno I'm having a problem that is probably simple to solve. I'm trying to get this code to print out "-30" but all I get is "0" no matter what value I put in "setpt[x]" in the serial.print line in loop().

```cpp
    typedef struct
    {
      byte var1[12];
      int var2[12];
      byte var3[12];
 
    }record_type;

    record_type profile[8];

    void setup()
    {
      Serial.begin(9600);
      Serial.println(" ***START***");
    }
    
    void loop()
    {
      profile[0] = (record_type) {(1,2,3),(-40,-30,-25),(1,2,3),};
      
      Serial.println(profile[0].var2[1]);
    
    }
```

---

<div class="post-metadata">

**Author:** ![christop](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christop/32/1013044_2.png) [@christop](https://forum.arduino.cc/u/christop)\
**Post date:** [March 29, 2022, 6:26pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/2 "2022-03-29T18:26:20Z")

</div>

> [@Dan\_Ward](#):
>
> ```arduino
> profile[0] = (record_type) {(1,2,3),(-40,-30,-25),(1,2,3),};
> 
> ```

This sets only the _first_ element of each array in your struct (with values 1, -40, and 1), leaving the remaining elements set to zero. Change the parentheses to braces inside the braces (with parentheses instead of braces, each expression evaluates to a single value—the commas are actually [comma operators](https://en.wikipedia.org/wiki/Comma_operator)).

---

<div class="post-metadata">

**Author:** ![Dan\_Ward](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dan_ward/32/1058773_2.png) [@Dan\_Ward](https://forum.arduino.cc/u/Dan_Ward)\
**Post date:** [March 29, 2022, 7:02pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/3 "2022-03-29T19:02:32Z")

</div>

> [@Dan\_Ward](#):
>
> `profile[0] = (record_type) {(1,2,3),(-40,-30,-25),(1,2,3),};`

Thanks ! that worked. profile[0] = (record\_type) { {1,2,3} , {-40,-30,-25} , {1,2,3} };

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 29, 2022, 7:26pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/4 "2022-03-29T19:26:29Z")

</div>

Why the cast to record\_type when the struct is declared to be of that type anyway ?

---

<div class="post-metadata">

**Author:** ![Dan\_Ward](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dan_ward/32/1058773_2.png) [@Dan\_Ward](https://forum.arduino.cc/u/Dan_Ward)\
**Post date:** [March 29, 2022, 7:41pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/5 "2022-03-29T19:41:08Z")

</div>

I see what you mean. I'm fairly new to this and just followed an example. Thanks for the hint.

---

<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:** [September 25, 2022, 7:42pm UTC](https://forum.arduino.cc/t/struct-containing-arrays/974937/6 "2022-09-25T19:42:00Z")

</div>

Closed on September 25, 2022, 7:42pm UTC

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