Week 07 Tutorial Sample Answers

  • How is everyone going with the assignment? Are you enjoying it?

    Where can you go to get help?

    Please seek help if needed. Frequent feedback is essential in the process of learning.

    You can ask questions on the course forum. You can ask questions in consultations. You can ask questions via emailing your teachers.

  • Write a function that takes in a 2D array of ints and multiplies every value in the array by a given int.

    It will have this prototype:

    void scalar_multiply(int rows, int columns, int matrix[rows][columns],  int scalar);
    
    void scalar_multiply(int rows, int columns, int matrix[rows][columns],  int scalar){
        int i = 0;
        while (i < rows) {
            int j = 0;
            while (j < columns) {
                matrix[i][j] = matrix[i][j] * scalar;
                j++;
            }
            i++;
        }
    }
    
  • What is a pointer? How can you declare and initialise a pointer?

    A pointer is a variable that can hold a memory address. It is always aimed at a particular variable type.

    A * is used to declare a variable as a pointer. For example, to declare a pointer to an int:

    int *pointer_name;
    

    A pointer is most often aimed at a particular variable. The & operator can be used to find the address of a variable.

  • What will happen when each of the following statements is executed (in order)?

    int n = 42;
    int *p, *q;
    p = &n;
    *p = 5;
    *q = 17;
    q = p;
    *q = 8;
    

    Filling out the follwing table may be helpful:

    Address int n = 42; int *p;
    int *q;
    p = &n; *p = 5; *q = 17; q = p; *q = 8;
    0xFF80
    0xFF84
    0xFF88
    0xFF8C
    0xFF90
    Note: Address lengths have been reduced for brevity.
    Address int n = 42; int *p;
    int *q;
    p = &n *p = 5; *q = 17; q = p; *q = 8;
    0xFF80
    0xFF84
    0xFF88
    0xFF8C
    0xFF90
  • What does the char *word input mean? What's the relationship between an array and a pointer?

    char *word means that the function takes in a pointer to a character as its input. A pointer is a variable which contains a memory address, but so is an array. This function in particular is expecting the character pointer to be telling us the address of the first character in an array of characters. Since this array of characters ends with a '\0', it is a string.

    There are some subtle differences between arrays and pointers in C. A pointer can have an address directly assigned to it but an array can not. You can only directly assign values to elements inside an array. Also, the sizeof operator will return the size of the pointer itself for a pointer, while the sizeof operator will return the total size of the array for an array.

  • What is a struct? What are the differences between structs and arrays?

    Arrays and struct are both compound data types, formed from other data types.

    Array are homogeneous - formed from a single data type.

    Structs can be heterogeneous - formed from multiple data types.

    Array elements are accessed with integer array indices.

    Struct members are accessed by name.

    Structs need to be defined outside of the main function before declaration.

  • This C code:

    int x;
    int a[6];
    
    x = 10;
    a[3 * 2] = 2 * 3 * 7;
    printf("%d\n", x);
    

    mysteriously printed 42. How could this happen when x is clearly assigned only the value 10?

    How does the output change if we create a another int after the array?

    How can you easily detect such errors before they have mysterious effects?

    The C code assigns to 42 to a[6] an array element that does not exist.

    The result of this is undefined.

    In this case the value 42 has apparently been assigned to the variable x. Silently changing another variable is a common consequence of use of an illegal array index.

    Subtler and more confusing behaviour are also quite possible.

    If you are using a CSE machine you can compile the program with dcc.

    It produces a program which will stop immediately with a clear error message if an invalid array index is used.