# Alternative to Union from Codesys

**URL:** <https://community.br-automation.com/t/alternative-to-union-from-codesys/6150>\
**Category:** Programming\
**Tags:** automation-studio, structured-text\
**Created:** [March 7, 2025, 8:49am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150 "2025-03-07T08:49:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kili\_Theiler](https://avatars.discourse-cdn.com/v4/letter/k/FF8800/32.png) [@Kili\_Theiler](https://community.br-automation.com/u/Kili_Theiler)\
**Post date:** [March 7, 2025, 8:49am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/1 "2025-03-07T08:49:19Z")

</div>

Has B&R in Automation Studio with structured text something similar to Union from Codesys?

 ![Screenshot 2025-03-07 094236](https://europe1.discourse-cdn.com/brcommunity/original/2X/9/94b1d2ae4ff0f513d4c2eca94a026fdbba2800b4.png)

---

<div class="post-metadata">

**Author:** ![alexander.hefner](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/alexander.hefner/32/111_2.png) [@alexander.hefner](https://community.br-automation.com/u/alexander.hefner)\
**Post date:** [March 7, 2025, 9:39am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/2 "2025-03-07T09:39:58Z")

</div>

Hi,

union is not supported right now (maybe with the release of ST OOP, which is coming with AS Code mid of this year).

But if I got it right, it’s more or less a structure where the elements are aligned to the same size, right? So, if declaring a structure with for example 4 byte data types as elements only, it should have the same behavior as described … or I understood the explanation completely wrong, which is possible of course 😉

Best regards!

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [March 7, 2025, 10:57am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/3 "2025-03-07T10:57:16Z")

</div>

Hi,

if the challenge is to save memory (e.g. a data base) one can simulate a union with a struct by storing also the data type and using dynamic variables to access the data.

```auto
TYPE
	myRecordTypeEnum : 
		(
		REC_DINT,
		REC_REAL
		);
	myRecord : STRUCT 
		type : myRecordTypeEnum;
		data : ARRAY[0..3]OF USINT;
	END_STRUCT;
END_TYPE

```

```auto
	IF write_dint THEN
		write_dint := FALSE;
		ddint ACCESS ADR(record.data);
		record.type := REC_DINT;
		ddint := 3;
	END_IF

	IF write_real THEN
		write_real := FALSE;
		dreal ACCESS ADR(record.data);
		record.type := REC_REAL;
		dreal := 3.1415;
	END_IF

	IF read THEN
		read := FALSE;
		CASE record.type OF
			REC_DINT: 
				ddint ACCESS ADR(record.data);
				(* do something with ddint *)
			REC_REAL: 
				dreal ACCESS ADR(record.data);
				(* do something with dreal *)
		ELSE
			(* invalid datatype *)
			
		END_CASE
	END_IF

```

```auto
VAR
	dreal : REFERENCE TO REAL;
	ddint : REFERENCE TO DINT;
	record : myRecord;
	write_dint : BOOL;
	write_real : BOOL;
	read : BOOL;
END_VAR

```

---

<div class="post-metadata">

**Author:** ![Matt\_Buck](https://avatars.discourse-cdn.com/v4/letter/m/FF8800/32.png) [@Matt\_Buck](https://community.br-automation.com/u/Matt_Buck)\
**Post date:** [March 7, 2025, 4:25pm UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/4 "2025-03-07T16:25:05Z")

</div>

Not currently as I don’t believe it’s a part of the IEC 61131 main standard. Per this blog post [IEC 61131-3: Additional language extensions – Stefan Henneken](https://stefanhenneken.net/2017/06/17/iec-61131-3-additional-language-extensions/), twin cat and codesys may support it in some fashion, and per Alex, it might be supported in ST OOP in Automation Studio Code. But currently it isn’t a part of the structured text language as implemented in Automation Studio. Automation Studio’s compliance with the IEC 61131-3 standard is documented in the AS help [B&R Online Help](https://help.br-automation.com/#/en/4/programming%2Fprogrammingmodel_compliancelist.html). I don’t see it listed as part of the standard.

You may be able to implement something in a library in C to handle this and then wrap it up nicely for structure text usage.

For other’s not familiar, a union defines multiple ways to access the same memory address with different formats. i.e. I can define a UNION of BYTE[4] and a DWORD and then be able to access each of the bytes of the DWORD via the BYTE array without having to do any ACCESS or memcpy(). (It’s a commonly used in C and other languages, but from what I can tell not a part of the 61131-3 standard, edition 3.)

---

<div class="post-metadata">

**Author:** ![Swee\_Hoe](https://avatars.discourse-cdn.com/v4/letter/s/FF8800/32.png) [@Swee\_Hoe](https://community.br-automation.com/u/Swee_Hoe)\
**Post date:** [March 10, 2025, 4:12am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/5 "2025-03-10T04:12:09Z")

</div>

Finally I see someone question about structured text implementation in Automation Studio. 🙂

I not sure `UNION` is covered in 61131-3: 2013 but I will say that it is better to have it.  
Secondly, I think 61131-3 did covered OOP but ST in B&R Automation Studio does not have it. I glad to hear that we will get it soon(ST OOP).

---

<div class="post-metadata">

**Author:** ![Kili\_Theiler](https://avatars.discourse-cdn.com/v4/letter/k/FF8800/32.png) [@Kili\_Theiler](https://community.br-automation.com/u/Kili_Theiler)\
**Post date:** [March 10, 2025, 7:52am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/6 "2025-03-10T07:52:34Z")

</div>

Thank you for all the replies. I use union mostly for conversions, for example, Modbus registers (status and control words) and PLC internal flags. I think Matt Buck’s approach with the C function will help me.

---

<div class="post-metadata">

**Author:** ![christoph.hilchenbac](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/christoph.hilchenbac/32/423_2.png) [@christoph.hilchenbac](https://community.br-automation.com/u/christoph.hilchenbac)\
**Post date:** [March 12, 2025, 7:21am UTC](https://community.br-automation.com/t/alternative-to-union-from-codesys/6150/7 "2025-03-12T07:21:02Z")

</div>

> [@Kili\_Theiler](#):
>
> I use union mostly for conversions

this is called ‘type-punning’ and there is a nice article on stackoverflow about it:

> <https://stackoverflow.com/questions/67206482/portability-of-using-union-for-conversion>

TLDR: Using Unions for “type punning” is fine in C, and fine in gcc’s C++ as well (as a gcc [g++] extension). But, “type punning” via unions has hardware architecture _endianness considerations_
