CMSC--202 , Section Computer Science II for Majors
Fall 1997 24 September 1997 Homework 3

Note that the usual ``midnight rule'' does not apply for this assignment. To be accepted, you must hand in your assignment before the end of the class meeting on the indicated date.

There is NO automatic extension. Homework not submitted by the end of the class meeting will not be accepted. Submittals are to be on paper, not electronic.

Assignment

  1. Write the type definitions, variable declarations, and code that will create the following picture:

  2. Write the type definitions, variable declarations, and code that will create the following picture:

  3. Write the type definitions, variable declarations, and code that will create the following picture:

  4. Write the type definitions, variable declarations, and code that will create the following picture:

  5. Draw the results of the following code:

    typedef struct st_tag * str_ptr;
    typedef struct st_tag
    {
      str_ptr field1;
      str_ptr field2;
    } struct_type;
    
    main()
    {
      str_ptr problem;
    
      problem = malloc(sizeof(struct_type));
      problem->field1 = malloc(sizeof(struct_type));
      problem->field2 = malloc(sizeof(struct_type));
      problem->field1->field1 = problem->field1;
      problem->field1->field2 = problem->field2;
      problem->field2->field1 = problem->field1;
      problem->field2->field2 = problem->field2;
    }
    

  6. Draw the results of the following code:

    typedef int *int_ptr;
    typedef int_ptr *int_ptr_ptr;
    typedef struct 
      {
       int_ptr_ptr field1;
       int_ptr_ptr field2;
      } problem_struct;
    
    main()
    {
      problem_struct problem;
    
      problem.field1 =  malloc(sizeof(int_ptr));
      *problem.field1 =  malloc(sizeof(int));
      **problem.field1 = 73;
      problem.field2 =  malloc(sizeof(int_ptr));
      *problem.field2 = *problem.field1;
      **problem.field2 = 42;
    }
    

  7. Draw the results of the following code:

    typedef int *int_ptr;
    typedef int_ptr *int_ptr_ptr;
    typedef struct 
       {
        int_ptr_ptr field1;
        int_ptr_ptr field2;
       } problem_struct;
    
    main()
    {
      problem_struct problem;
    
      problem.field1 =  malloc(sizeof(int_ptr));
      *problem.field1 =  malloc(sizeof(int));
      **problem.field1 = 73;
      problem.field2 =  malloc(sizeof(int_ptr));
      *problem.field2 =  malloc(sizeof(int));
      **problem.field2 = 42;
    }
    

  8. Draw the results of the following code:

    typedef int *iptr;
    typedef struct strtag *stptr;
    typedef struct strtag 
      {
       iptr field1;
       stptr field2;
      } strtype;
    typedef stptr arrtype[2];
    
    main()
    {
      arrtype x;
    
      x[0] =  malloc(sizeof(strtype));
      x[0]->field2 = x[0];
      x[1] =  malloc(sizeof(struct strtag));
      x[1]->field2 = x[0]->field2->field2;
      x[1]->field1 =  malloc(sizeof(int));
      x[1]->field2->field1 = x[1]->field1;
      *(*x[0]).field1 = 1776;
      x[1]->field2->field2->field2 = x[1];
      *x[0]->field2->field1 = 2001;
    }
    

Submitting the Homework

This homework is to be submitted on paper, not electronically. You may submit your pictures handwritten on paper. Your code must be submitted typewritten on paper. You may find it helpful to compile and run each code sequence you write. Doing so will not guarantee that your answers are correct, but at least it will reassure you that you do not have any glaring errors. Please place your name and student ID number at the top of every page of your homework. Special Notes:



Thomas A. Anastasio
Thu Sep 25 00:01:27 EDT 1997