CS140 Lab 6 - C Programming Refresher

Objective

  • Refresh (or learn for the first time) some details needed in C
  • Pointers, Memory addresses, structs, dynamic memory

Background

You will need various online resources to complete this lab:

  • Programming in C A Tutorial - Kernighan
  • Tutorial on C, Makefiles, gdb
  • C for Java Programmers.
  • The C Programming Language, by Kernighan and Ritchie
  • Tutorial Tasks

    1. Start with the Lab-Tutorial document here. Program the examples. Create the makefile and compile with make. Do the very brief gdb tutorial.
    2. Next, spend some time with Kernighan's C tutorial (linked above). Experiment with anything you don't already know well. Pay special attention to sections 17-20.

    Programming Tasks

    1. Create a program with the structure defined in Kernighan's tutorial section 20.
    2. Add to the show_bytes.c some code to display the bytes of the struct.
    3. Load a stuct variable with some data (that you can predict what it looks like) and use show_bytes to verify that it does what you expect.
    4. Print out the size in bytes of the struct.
    5. Print out the memory address of the struct and each of it's data members.
    6. Print out the memory offset of each of the data members of the struct.
    7. Create an array of structs using malloc

    Linked Lists:
    1. Create a struct to act as a linked list node that holds integers.
    2. Write utility method to malloc nodes
    3. Write methods to add/remove nodes from the head, find the length of a list, and iterate through a list printing out each value.

    Check out



    When you are done with the lab exercises, check out with the instructor or the TA.