Week 01 Tutorial Questions

  1. Class introduction (for everyone, starting with the tutor):

    • introduce yourself to the other people in the tutorial
    • discuss what you learned from COMP1511/COMP1911/COMP1917
    • discuss what parts you're still unsure of
  2. Consider the following small C program:

    #include <stdio.h>
    
    int main(void) {
        int n[4] = { 42, 23, 11, 7 };
        int *p;
    
        p = &n[0];
        printf("%p\n", p); // prints 0x7fff00000000
        printf("%lu\n", sizeof (int)); // prints 4
    
        // what do these statements print ?
        n[0]++;
        printf("%d\n", *p);
        p++;
        printf("%p\n", p);
        printf("%d\n", *p);
    
        return 0;
    }
    

    Assume the variable n has address 0x7fff00000000.

    Assume sizeof (int) == 4.

    What does the program print?

  3. What is the output from the following program and how does it work? Try to work out the output without copy-paste-compile-execute.

    #include <stdio.h>
    
    int main(void) {
        char *str = "abc123\n";
    
        for (char *c = str; *c != '\0'; c++) {
            putchar(*c);
        }
    
        return 0;
    }
    
  4. Consider the following struct definition defining a type for points in a three-dimensional space:

    typedef struct Coord {
        int x;
        int y;
        int z;
    } Coord;
    

    and the program fragment using Coord variables and pointers to them:

    {
        Coord coords[10];
        Coord a = { .x = 5, .y = 6, .z = 7 };
        Coord b = { .x = 3, .y = 3, .z = 3 };
        Coord *p = &a;
    
        /*** A ***/
        (*p).x = 6;
        p->y++;
        p->z++;
        b = *p;
        /*** B ***/
    
    }
    

    1. Draw diagrams to show the state of the variables a, b, and p, at points A and B.

    2. Why would a statement like *p.x++; be incorrect?

    3. Write code to iterate over the coords array using just the pointer variable p the address of the end of the array, and setting each item in the array to (0,0,0). Do not use an index variable.

  5. Explain the differences between the properties of the variables s1 and s2 in the following program fragment:
    #include <stdio.h>
    
    char *s1 = "abc";
    
    int main(void) {
        char *s2 = "def";
        // ...
    }
    

    Where is each variable located in memory? Where are the strings located?

  6. What is the effect of each of the static declarations in the following program fragment:

    #include <stdio.h>
    
    static int x1;
    ...
    
    static int f(int n) {
        static int x2 = 0;
        ...
    }
    
  7. What is the difference in meaning between the following pairs (a/b and c/d) of groups of C statements:
    1. if (x == 0) {
          printf ("zero\n");
      }
      
    2. if (x == 0)
          printf ("zero\n");
      
    3. if (x == 0) {
          printf ("zero\n");
          printf ("after\n");
      }
      
    4. if (x == 0)
          printf ("zero\n");
          printf ("after\n");
      
  8. C functions have a number of different ways of dealing with errors:

    • terminating the program entirely (rare)
    • setting the system global variable errno
    • returning a value that indicates an error (e.g., NULL, EOF)
    • setting a returning parameter to an error value

    They might even use some combination of the above.

    Think about how the following code might behave for each of the inputs below. What is the final value for each variable?

    int n, a, b, c;
    n = scanf("%d %d %d", &a, &b, &c);
    

    Inputs:

    1.   42 64 999
    2.   42 64.4 999
    3.   42 64 hello
    4.   42 hello there
    5.   hello there
  9. Consider a function get_int() which aims to read standard input and return an integer determined by a sequence of digit characters read from input. Think about different function interfaces you might define to deal with input that is not a sequence of digits, or that is a very long sequence of digits.

  10. For each of the following commands, describe what kind of output would be produced:

    1. gcc -E x.c
    2. gcc -S x.c
    3. gcc -c x.c
    4. gcc x.c

    Use the following simple C code as an example:

    #include <stdio.h>
    #define N 10
    
    int main(void) {
        char str[N] = { 'H', 'i', '\0' };
        printf("%s\n", str);
        return 0;
    }
    
  11. Consider the stack, queue, and priority queue data structures. Show the state of each of these structures after the following operations are performed. Assume that all of the structures start as empty, and that the priority queue is ordered by higher values having higher priorities.

    insert 5
    insert 7
    insert 3
    insert 4
    remove
    remove
    insert 6
    insert 1
    remove
    insert 9
    
  12. Consider the following FIFO queue interface in a file Queue.h:

    #ifndef QUEUE_H
    #define QUEUE_H
    
    #define MAXQ 6
    
    typedef struct {
    	int nitems;
    	int head;
    	int tail;
    	int items[MAXQ];
    } Queue;
    
    // initialise a Queue
    void initQueue (Queue *q);
    // insert a new item at the tail of the Queue
    void enterQueue (Queue *, int);
    // remove/return the item at the head of the Queue
    int leaveQueue (Queue *);
    // return the number of items currently in the Queue
    int lengthQueue (Queue);
    // display the contents of the Queue
    void showQueue (Queue);
    
    #endif
    

    Using the above, answer the following questions:

    1. What is the purpose of the lines involving the symbol QUEUE_H?

    2. Why do some functions take a pointer to a Queue, and others take a Queue, as an argument?

    3. Implement the operations on the queue, treating it as a "circular array" (i.e., when the head or tail reaches the end of the array, they advance by returning to the start of the array). The following diagram shows the state of the queue after the following sequence of enters and leaves: +1 +4 +5 +7 - - +6 +9 - +2

    4. Implement the operations on the queue, treating it as a "sliding array" (i.e., when an element is taken from the head, all of the items slide down by one position, and so item 0 is always the head). The following diagram shows the state of the queue after the following sequence of enters and leaves: +1 +4 +5 +7 - - +6 +9 - +2

    5. Can you simplify the concrete representation when using a "sliding array"?