# Programming in C and C++ (2018/19)
Supervision 1

## 1

What is the difference between `'a'` and `"a"`?

## 2

Does the following code terminate? And if so, under which conditions?

        char i, j;
        for (i = 0; i < 10, j < 5; i++, j++);

## 3

Write an implementation of bubble sort for a fixed array of integers.

An array of integers can be defined as `int i[] = {1, 2, 3, 4};`

Modify youur answer to sort characters into lexicographical order.

Use function recursion to write an implementation of merge sort for a fixed array of integers; how much memory does your program use for a list of length n?

## 4

Implement a function `int cntlower(char str[])` that, given a string, returns the number of lower-case letters it contains.

## 5

Define a macro `SWAP(t, x, y)` that swaps two arguments `x` and `y` of type `t`. Does your macro work as expected for

    SWAP(int, v[i++], w[f(x)])

What other invocations could cause a simple implementation to fail and how can we mitigate them?

## 6

Define a macro `SWAP(x, y)` that swaps `x` and `y` of the same type without using any extra memory.

## 7

Given a pointer `p`, what does the expression `p[-2]` mean, and when is it legal?

## 8

If `p` is a pointer to a structure, write some C code which uses all the following indirections:

        ++p->i
        p++->i
        *p->i
        *p->i++
        (*p->i)++
        *p++->i

and describe the action of each code snippet.

## 9

Write a string search function with a declaration of

        char *strfind(const char *s, const char *f);

which returns a pointer to the first occurrence of `s` in `f` (and `NULL` otherwise)

## 10

Write a program `calc` which evaluates an expression in Reverse Polish Notation given on the command line; for example

        $ calc 2 3 4 + *

should print 14.

## 11

What is the value of `i` after executing each of the following:

1. `i = sizeof(char);`
2. `i = sizeof(int);`
3. `int a; i = sizeof a;`
4. `char b[5]; i = sizeof(b);`
5. `char *c=b; i = sizeof(c);`
6. `struct { int d; char e;} s; i = sizeof s;`
7. `void f(int j[5]) { i = sizeof j;}`
8. `void f(int j[][10]) { i = sizeof j; }`

## 12

According to the rules of pre and post increment what would you expect these expressions to do:

        i++ + ++i
        ++i + ++i

What actually happens if you try it?

## 13

A spacecraft arrives on Mars, but its memory has been corrupted by radiation en route. Luckily, it can receive updates one bit at a time using a predefined C function `short receive_bit(void)`, that when called will return either 1 or 0. The stream of bits for a value is transmitted in unsigned big-endian byte order: for example, a 16-bit value of 125 would be 0000000001111101. Assume the int type is 32 bits.

1. Explain the meaning of the `inline` keyword on C function declarations, and a potential drawback of using it.

2. Using `receive_bit()`, define a function `receive_int()` that decodes and returns a 32-bit value from the sequence of received bits.

3. (optional) Build a more general decoding function receive using a C++ template with two parameters that specify the number of bits to decode and a datatype for the decoded value. Use this to write two template instantiations that decode an 8-bit value into a short and a 32-bit value into an unsigned long.

4. Find and explain four instances of undefined behaviour that could result from compiling and running the C code below with different command-line arguments. The `strcpy(dst,src)` function copies a zero-terminated C string from the `src` buffer to the `dst` buffer. The `putchar(c)` function outputs a character c to the console. You can assume that the standard C header prototypes have been included for `<stdio.h>`, `<stdlib.h>` and `<string.h>`.

               char *show_instruction(int msg) {
                   char buf[6];
                   int fuel;
                   if (msg == 1 && fuel--) {
                       strcpy(buf, "THRUST");
                       return buf;
                   } else if (msg == 2) {
                       char *msg = (char *)malloc(100);
                       strcpy(msg, "DEPLOY_PARACHUTE");
                       return msg;
                   }
               }

               int main(int argc, char **argv) {
                   char *msg;
                   msg = show_instruction(argc);
                   putchar(msg[0]);
                   return 0;
               }