## Instructions

1. This homework will help give you practice with:

1. structs,
2. typedefs, and
3. pointers

2. Print out your answers and hand them in at the start of class on the assigned date.

3. Please use either a text editor, like vi, emacs, TextEdit on the Mac, or TextPad on a Windows PC, or else a word processor like Microsoft Word to create your answers. You may print out a copy of this homework and answer question 4 and the "fill in the memory diagram" part of question 5 using the printed homework sheets.

## Questions

1. Behold the following declaration:
```    #define MAX_NAME_SIZE 20
typedef struct {
char part_name[MAX_NAME_SIZE];
int part_id;
int quantity_on_hand;
double cost;
} Part;
```

1. Declare a Part variable named "my_part" and initialize it with the values "water filter", 368, 45, 49.99.

2. What is the rationale for declaring 20 as a constant, rather than hard-coding it in the declaration of the array?

3. Write a scanf statement that reads a floating point number from stdin into the cost field of my_part.

4. Write a scanf statement that reads a string into the part_name field of my_part.

2. Design an anonymous struct and use a typedef to name it SupplierAddress. It should have fields named street_address, city, state, and zip_code. The street address and the city should be C-strings with a maximum length of 30 characters, the state should be a two character C-string, and the zip code will be an integer.

1. 0xff4e7a98 + 0x4 = _______________ (I want the hexadecimal result)

2. 0xfe93ab86 + 0x95 = ________________ (I want the hexadecimal result)

3. Put a check mark next to any of the following statements that is illegal. Put an 'X' next to any of the following statements that might eventually cause a segmentation violation or bus error. Consider each statement in isolation and assume that all variables have already been initialized to valid values. If there is more than one statement on a line consider the statements as a group (e.g., if the combined effect of the statements might be to eventually cause a segmentation violation, then put an 'X' next to the statements).

```       char name[20];
char first_name[10];
char last_name[20];
char *str_ptr;

_________  name = str_ptr;

_________  str_ptr = name;

_________  if (name == "brad") { ... }

_________  name = "sue";

_________  first_name = strdup("tom");

_________  strcpy(name, first_name); strcat(name, last_name);

_________  name[10] = 'c';

_________  str_ptr[10] = 'c';
```
4. Consider the following code:

```     int count;
char word[7];
char *string_ptr;
int *int_ptr;
int **double_int_ptr;

strcpy(word, "puppy");
count = strlen(word);

string_ptr = word;
int_ptr = &count;
double_int_ptr = &int_ptr;

1)   printf("%s %d\n", word, count);
2)   printf("0x%x 0x%x\n", int_ptr, double_int_ptr);
3)   printf("0x%x\n", string_ptr);
```

Further suppose that the above variables are stored at the following memory addresses:

```     address of count = 0xffbef98c
```

1. What is the output of printf statement (1)?
2. What is the output of printf statement (2)?
3. What is the output of printf statement (3)?
4. What is the value of *double_int_ptr?
5. What is the value of **double_int_ptr?
6. In the following memory diagram, do the following two things:
1. to the left of the memory location, write the name of the variable, if any, that is stored at that location, and
2. in the diagram, write the contents of that memory location.

If there is no variable stored at a location, leave the line blank. If the contents of a memory location are unknown, put a ? in the location.

```	                                ---------------------------
0xffbef974 __________________ |                         |

0xffbef978 __________________ |                         |

0xffbef97c __________________ |                         |

0xffbef980 __________________ |                         |

0xffbef984 __________________ |                         |

0xffbef988 __________________ |                         |

0xffbef98c __________________ |                         |
---------------------------

```
5. Go to here and show what the output for test1.c, test2.c, and test3.c should be. You do not have to worry about test4.c. Note that you cannot compile these programs and execute them to produce the correct output since the programs will not have the variable s placed at location 0xefffe4f8 or 0xefffe4f0.

6. The programs debug1.c, debug2.c, and debug3.c each has an error that programmers commonly make. First compile each of the programs and run them. For debug2, try giving it the input strings "hi-brad" and "the-game-is-over". For debug3 try giving it a number of strings, all of whose length is less than 20 characters. For each program, locate the bug and 1) describe what is causing the bug, and 2) describe a possible way to fix the bug. In debug1 you may assume that the size of the largest string to be input is 30 characters. You should assume in debug2 and debug3 that the user always inputs strings that are the appropriate size (e.g., in debug2 the size of the two input strings will be less than 10 characters and 20 characters respectively).