Computer Science 381
Programming Unix in C
Winter Immersion 2016, The College of Saint Rose
In this lab, you will gain more experience using arrays and pointers in C.
Reference solutions to all programs are available on mogul in /home/cs381/labs/pointers2.
Arrays, Arrays, Arrays
Have a look at this example, which demonstrates some of the ways we can declare, construct, initialize, and otherwise use arrays in C.
See Example:
/home/cs381/examples/arrays
The comments in this program describe its usage of the most important C features. Pay special attention to the usage of malloc to allocate chunks of memory and free to return them to the system when finished. It also shows the use of realloc to change the size of an allocated chunk of memory.
This demonstrates one of the key differences between C and Java: we have to tell C when we are finished with our allocated memory. Java uses garbage collection to reclaim memory no longer in use automatically. We will consider the merits of both approaches later in the semester; for now we simply need to remember that any memory we allocate in C must be released when we are done with it. Advice: when you add a malloc(), immediately add the corresponding free() in an appropriate place.
Strings
We have seen that strings in C are simply represented as NULL-terminated arrays of char. The following example demonstrates more about this and gives examples of some of C's string processing functions.
See Example:
/home/cs381/examples/strings
Also read the man page for string(3). It lists the string processing functions available in the standard C library.
Pointers to Arrays and Multidimensional Arrays
Read Sections 5.6-5.10 of K&R.
Submission
Please submit all required files as email attachments to terescoj AT strose.edu by Thursday, December 31, 2015. Be sure to check that you have used the correct file names and that your submission matches all of the submission guidelines listed on the course home page. In order to email your files, you will need to transfer them from mogul to the computer from which you wish to send the email. There are a number of options, including the sftp command from the Mac command line.
Grading
This lab is graded out of 30 points.
Grading Breakdown | |
Lab questions | 14 points |
Practice program inputsortadder.c correctness | 10 points |
Practice program strend.c correctness | 6 points |
Total | 30 |