# 3.70 Union

Consider the following union declaration:

```c
union ele {
    struct {
        long *p;
        long y;
    } e1;
    struct {
        long x;
        union ele *next;
    } e2;
};
```

This declaration illustrates that structures can be embedded within unions.

The following function (with some expressions ommitted) operates on a linked list having these unions as list elements:

```c
void proc(union ele *up) {
    up->____ = *(____) - ____;
}
```

A. What are the offsets (in bytes) of the following fields?

| field     | offset |
| --------- | ------ |
| `e1.p`    | 0      |
| `e1.y`    | 8      |
| `e2.x`    | 0      |
| `e2.next` | 8      |

B. How many total bytes does the structure require?

16 bytes.

C. The compiler generates the following assembly code for `proc`:

```
proc:
    movq     8(%rdi), %rax
    movq     (%rax), %rdx
    movq     (%rdx), %rdx
    subq     8(%rax), %rdx
    movq     %rdx, (%rdi)
    ret
```

On the basis of this information, fill in the missing expressions in the code for `proc`. *Hint*: Some union references can have ambiguous interpretations. These ambiguities get resolved as you see where the references lead. There is only one answer that does not perform any casting and does not violate any type constraints.

```c
void proc(union ele *up) {
    up->e2.x = *(*(up->e2.next).e1.p) - *(up->e2.next).e1.y;
}
```


---

# 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://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/3.-machine-level-representation-of-programs/3.70-union.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.
