1. You are given the following declaration:
         typedef struct person {
             char name[20];
    	 int age;
    	 double salary;
         } Employee;
         
         Employee *brad;
         
    Write a line of code that will allocate a person struct to which brad will point:
          brad = 
          

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

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

  2. 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';
           
  3. 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
         address of word = 0xffbef980
         address of string_ptr = 0xffbef97c
         address of int_ptr = 0xffbef978
         address of double_int_ptr = 0xffbef974
         

    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 __________________ |                         |
      	                                ---------------------------
      
           
  4. 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.

  5. 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).