CS-220 Spring 2018 Lab 4 - Pointers
Today's lab investigates pointers in C. I have provided a basic infrastructure in pointers.c. Download this file into a lab04 sub-directory. The pointers.c file contains several things:
- Some #includes to enable use of standard library functions for IO.
- Function declarations for three functions you will need to write... increment, swap_temp, and swap_notemp. These functions need to do the following:
- increment takes a pointer to an integer as an argument. This function should add 1 to the value of the integer pointed to by the argument. The function does not return a value.
- swap_temp This function takes two integer pointers as arguments... a is a pointer to the first value, and b is a pointer to the second value. This function should put the value that a is pointing at into the integer that b is pointing at, and put the value that b is pointing at into the integer that a is pointing at. In order to do this, the swap_temp function may use a temporary integer variable.
- swap_notemp This function is exactly the same as swap_temp, except you may not use a temporary variable to hold the value of one of the integers. There are several ways to do this, but the simplest way is to loop through all 32 bits of the values pointed at by a. If a particular bit is on, then set that same bit in the a value to whatever that bit is in the b value, and set that bit in the b value on. If the bit in the a value is off, do the same thing, but set the bit in the b value off. You may use the setBit and getBit functions to help you.
- Function declarations for two functions I have provided for you if you want to use them, getBit, and setBit. The getBit function returns the value of the bit (0 or 1) specified by ind (which should be a number between 0 and 31... the indexes of all the bits in an integer) in the value pointed at by in. The setBit function updates the value pointed at by in by setting the bit at index ind to the value specified by the val argument, without changing any of the other bits in the value.
- A main function. You need to modify the main function by replacing the comments that say things like /* put correct expression here */ with the specific values required. For instance, the first printf statement reads:
printf("argc is at location %p\n",/* put correct expression here */);
You need to replace the comment with the expression that calculates the location of the argc parameter. There are also comments that define when to invoke the functions that you need to write. Please do not change the format of the printf statements or add new printf statements. Your grade will depend on whether your program prints the correct results or not, and extra output is incorrect.
- There is a comment after the main function that shows where your increment, swap_temp, and swap_notemp functions should be coded.
- The definitions of the getBit and setBit functions also appear. These are correctly coded and do not need to be modified, but you may want to look at this code to see how bit-wise AND and OR, as well as bit shifts can be useful.
Your job is to code the three functions required, modify the main function as required, and make sure the code runs correctly.
Submitting Your Results
Upload your revised pointers.c file in myCourses under CS-220 Content Lab Submissions, Lab 04 Submission before midnight Tuesday evening.
If you make a mistake submitting your lab, you may resubmit as often as you would like to. The TA's will only grade the last submission you make.
Grading your Results
Each lab submission is worth 10 points. Deductions will occur for the following reasons:
- Lab submissions are due at 11:59 PM on the day of the lab (Tuesday). If you submit late without an extension, there will be a three point deduction for every 24 hours you are late. Extensions are available only in special circumstances, and can only be given by the instructor or a TA.
- There will be a three point deduction for submissions that do not follow the required format. For instance, if you upload a file with the wrong file name, or your upload the wrong file.
- Your code is subject to the following deductions:
- There will be a seven point deduction if there are compiler errors when compiling your code.
- There will be a five point deduction if your code does not run to completion.
- There will be a two point if the compiler issues a warning message when compiling your code on an LDAP machine.
- There will be a one point deduction for each incorrect line printed by your revised pointers.c program. (Note that the addresses of parameters, variables, and array elements may vary slightly, depending on how you code your functions, and will be different on different machines, but the values should be exactly correct.)