1. Goals for this week

  1. Learn about command line arguments in C and the atoi function

  2. Reminder of file I/O (fopen, fclose, fscanf)

  3. Some reminders about dynamically allocated 2-D arrays in C, and about using gdb and valgrind for debugging C programs

  4. Introduction to Lab 7.

2. Starting Point Code

Start by creating the week08 directory in your WeeklyLabs subdirectory and copying over some files:

$ cd ~/cs31/WeeklyLabs
$ mkdir week08
$ cd week08
$ pwd
/home/you/cs31/WeeklyLabs/week08
$ cp ~newhall/public/cs31/week08/* ./
$ ls
Makefile  bad_cells.txt commandlineargs.c  twoDarray.c

3. C command line arguments and atoi

Let’s look at an example C program that takes command line arguments. Start by opening commandlineargs.c in an editor:

$ code commandlineargs.c

The first thing to note is the change in the definition of main:

int main(int argc, char **argv) { ...

The first parameter to main, argc, is the number of command line arguments. For example, if the user enters:

$ ./a.out 10 11 200

argc will be 4 (a.out counts as one of the command line arguments, and 10, 11, and 200 as three more). Each command line argument is passed to main in the second parameter, argv, as a string (argv is an array of strings):

        -----
argv[0]:| *-|-----> "./a.out"
        -----
argv[1]:| *-|-----> "10"
        -----
argv[2]:| *-|-----> "11"
        -----
argv[3]:| *-|-----> "200"
        -----
argv[4]:| *-|-----| (NULL pointer: indicates no more command line strings)
        -----

C has functions that can convert strings of numeric characters to their int, float, and other basic types, values. For example, atoi converts a string to an int:

int x = atoi(argv[1]);  // x gets the int value 10

See the man page for atoi for more information

$ man atoi

Let’s try compiling and running this program with different command line arguments and see what happens.

4. File I/O in C

For this next lab assignment you will be doing C file I/O using fopen, fclose, fscanf, etc.

As a reminder, you can look back at the fileio.c program from Weekly Lab Meeting 5 and try it out.

5. Reminder about gdb and valgrind for C program debugging

We’re back to C programming this week, with pointers and functions. Remember to use layout src to view C source code and navigate using next, step, and cont.

You are all experts at using GDB to debug at the assembly level, but remember that you also know how to use GDB (and Valgrind) to debug programs at the C code level.

Previously in lab, we looked at some examples of using both gdb and valgrind. Take a look at the Weekly Lab Meeting 2 page and the examples we talked about as a reminder of how to use gdb and the Weekly Lab Meeting 5 page for how to use valgrind.

6. Dynamically Allocated 2-D Arrays in C

We are not going to look at this program together in lab today, but the twoDarray.c program contains examples of C statically declared 2D arrays and dynamically allocated 2D arrays (using a single malloc).

As you work on Lab 7, you can refer to this example program dynamically allocate and accesses a 2D array.

7. Lab 7

Finally, let’s take a look at Lab 7.

8. Handy References