As you have been learning, pointers and arrays introduce various problems into programs. That is, debugging these programs can be difficult. This lab will give you additional opportunities to debug programs written using pointers and arrays.
Each of the functions (including the main) below may have problems in it. Your task is to determine where the problems are and correct these problems. We have not told you where the problems are exactly, and each of the problems may not cause an overt error. That is, the program may or may not act unusually (or produce a syntax error in the compiler) because of the problem.
The main program is intended to test the functions that are implemented. The functions should work as documented. When there are choices in chaning the program code, make the changes so that the functions are implemented as documented and so that the main program tests the functions adequately.
The program can be obtained from this link.
It is also listed here:
#include <stdio.h>
#include <string.h>
#define NUMBER_OF_INTS 5
// add 1 to the value pointed to by a
void increment(int *a)
{
*a++;
}
// subtract 1 from the value pointed to by a
void decrement(int *a)
{
*a--;
}
// This is supposed to re-implement the string concatenation function
// that we talked about in large lecture.
char *mystrcat(char *s1, char *s2)
{
char *copyS1 = *s1;
// Just in case s1 or s2 is not a valid pointer
if ((s1 == NULL) || (s2 == NULL)) return NULL;
while (*s1 != NULL) {
s1++;
}
while (*s2 != NULL) {
s1 = s2;
s1++;
s2++;
}
*s1 = NULL;
return *copyS1;
}
// Function to read a series of at most N integers from a file stream. Stops reading
// when N integers have been read, or a non-digit character (other than white
// space) is found or when EOF is reached. Considers white space to separate integers.
// Returns the number of integers successfully obtained.
// Places the integers, in the order obtained from the file into the array in list.
// Assumes N > 0.
int getints(FILE *fp, int *intlist, int N)
{
int result;
int numberValues; // number of int values obtained from fp
do {
result = fscanf(fp, "%d", intlist[numberValues]);
if (result == 1) {
numberValues++;
}
} while ((result == 1) && (numberValues <= N));
return numberValues;
}
// Computes a checksum of all the characters in the array by summing
// the values of all of the characters together.
// Doesn't worry about overflows. That is, doesn't worry about the sum
// having a total larger than can be stored in an int.
int checksum(char *s)
{
int length = strlen(s);
int check=0;
int i;
for (i=1; i <= length; i++) {
check += s + i;
}
return check;
}
int main()
{
char string1[100];
char string2[100] = "abc";
int intArray[4];
int result;
int i, first;
int value1 = 0;
int value2 = 0;
for (i=1; i <= 50; i++)
increment(&value1);
for (i=1; i <= 50; i++)
decrement(&value2);
printf("value1 = %d, value2 = %d\n", value1, value2);
printf("This checksum should be 32: %d\n", checksum(" "));
printf("This checksum should be 10: %d\n", checksum("\n"));
printf("This checksum should be 42: %d\n", checksum(" \n"));
// Sometimes concatenation is denoted by a "+" operator
printf("string1+string2= %s\n", mystrcat(string1, string2));
printf("Enter %d integers separated by blanks/newlines: ", NUMBER_OF_INTS);
result = getints(stdin, intArray, NUMBER_OF_INTS);
if (result != 5) {
printf("Could not get %d integers!\n", NUMBER_OF_INTS);
} else {
printf("The integers are: ");
first = 1;
for (i=0; i <= NUMBER_OF_INTS; i++) {
if (! first) printf(",");
else first = 0;
printf(" %d", intArray + i);
}
printf("\n");
}
fflush(stdin);
getchar();
return 0;
}
Turn in a hardcopy of the final program with the output given when you run it.