notes/education/software development/ECE1400/Chapter 17 Exercises.md
2024-11-19 20:57:20 -07:00

41 lines
1.1 KiB
Markdown

> **4.** Suppose that the following declarations are in effect:
```c
struct point {int x, y; };
struct rectangle { struct point upper_left, lower_right; };
struct rectangle *p;
```
> Assume that we want `p` to point to a rectangle structure whose upper left corner is at $(10, 25)$, and whose lower right corner is at $(20, 15)$. Write a series of statements that allocate such a structure and initialize it as indicated.
```c
struct rectangle rect = { { 10, 25 }, { 20, 15 } };
p = ▭
```
---
> **5.** Suppose that `f` and `p` are declared as follows:
```c
struct {
union {
char a, b;
int c;
} d;
int e[5];
} f, *p = &f;
```
> Which of the following statements are legal?
(a) `p->b = ' ';`
(b) `p->e[3] = 10;` - **Legal**
(c) `(*p).d.a = '*';` - **Legal**
(d) `p->d->c = 20;`
---
> **7.** The following loop is supposed to delete all nodes from a linked list and release the memory that they occupy. Unfortunately, the loop is incorrect. Explain what's wrong with it and show how to fix the bug.
```c
for (p = first; p != NULL; p = p->next)
free(p);
```
The above loop won't function because it deallocates an entry, resulting