Main Contents Page
CTEC1401 Programming in C
Quick Reference
Week 9: Arrays
Week 10: Pointers I
Week 11: Pointers II
  1. Before the lab session read CFTTP Chapter 8 and study the solved exercises very carefully.

  2. Work through the solved exercises 8.1 to 8.14 on pages 157 to 162. You should enter each program and then check the program works as described. Look at the answers given in the text to ensure that you understand how each program is behaving.

    Please note: This exercise may seem like a lot of "copying" from the book. However, the point here is not to "type the exercises and get them working" but rather to type them in and understand how they are working. Pointers are fundamental to C and practice typing the syntax and understanding the code is a crucial part of understanding how they work. Make sure you fully understand each program before you move on to the next. Read the explanation in the book that follows each example carefully - if you do not understand anything ask your lab tutor.

  3. ptr1

    Copy the following program into ptr1.c

    #include <stdio.h>
    
    int main()
    {
        int i   = 100;
        int *p  = &i;
        *p = 200;
        printf("i = %d\n", i);  
        return 0;
    }
    
    Modify the program so that it has an extra integer pointer variable q which is also initialised to the address of i. Add code before the return statement that doubles the variable i but you must do this by dereferencing your pointer variable q (ie do not refer to i directly). Then print out the value of i.

  4. ptr2

    Copy the following program into ptr2.c

    #include <stdio.h>
    int main()
    {
        int i = 200, *p, *q;
        p = &i;  q = p;
        *q = *q + 1;
        printf("*p = %d\n", *p); 
        return 0;
    }
    
    Modify the program by adding another integer pointer variable r which is also an alias for variable i. Add print statements to output the dereferenced values of q and r.

  5. ptr3

    Copy the following program into ptr3.c.

    #include <stdio.h>
    int main()
    {
        int i, j;
        int * p, * q;
        int ** x;
        
        i = 100;
        j = 200;
        p = &i;
        q = &j;
        x = &p;
        
        *p = *p + *q;
        *q = **x / 2;
        **x = *p + j;
        
        printf(" i = %d\n",   i);
        printf("&i = %p\n",  &i);
        printf(" j = %d\n",   j);
        printf("&j = %p\n",  &j);
        
        printf(" p = %p\n",   p);
        printf("&p = %p\n",  &p);
        printf("*p = %d\n",  *p);
        printf(" q = %p\n",   q);
        printf("&q = %p\n",  &q);
        printf("*q = %d\n",  *q);
        
        printf(" x = %p\n",   x);
        printf("&x = %p\n",  &x);
        printf("*x = %p\n",  *x);
        printf("**x= %d\n", **x);
    
        return 0;
    }
        

    Study this code carefully and make sure you understand how the program works. Then modify the program by adding a new variable that stores the address of x. Then use your variable to update (indirectly) the value of i and then print out the new value to demonstrate that your modification has worked.