# Union tutorial

Suppose the source text looked like this:

```
#include <stdlib.h>

union urecord_t
{
  char c;
  short s;
  long l;
};

struct record_t
{
  int type;
#define RTYPE_CHAR      0
#define RTYPE_SHORT     1
#define RTYPE_LONG      2
  urecord_t u;
};

bool is_negative(record_t *r)
{
  switch ( r->type )
  {
    case RTYPE_CHAR:  return r->u.c < 0;
    case RTYPE_SHORT: return r->u.s < 0;
    case RTYPE_LONG:  return r->u.l < 0;
  }
  abort();
}
```

We have a disassembly like this:

![](/files/sNWfpqWdNxetIyjhO5S6)

Let’s improve it with unions. First, let’s define an union type. For this we open the **Local types** view (menu View|Local types), press Ins to create an union.

We create the union using the "C syntax" tab of the "Add type" dialog:

![](/files/C1SM4N2sHg8XNDnU6N7P)

Switching to the disassembly window, we apply the defined structure through the **Edit|Operand types|Struct offset** menu item and select the proper representation for the operand. In the union type case, it may be necessary to select the desired union member with the **Edit|Structs|Select** union member command. The final disassembly looks like this:

That’s all folks !


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hex-rays.com/core/disassembler/how-tos/union-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
