Arrays can be declared with an explicit size:
char buffer[MAXSIZE+1] ;
// hold a string of at most MAXSIZE characters + terminating NUL
('\0')
Arrays can be initialized, which sets the array's size and
the initial contents:
char mesg[] = "Hello!" ; // a 7 element array - 6 characters
in Hello! + terminating NUL
An array name is a constant pointer to the first (0th)
array element; thus:
mesg
== &mesg[0] ; // address of the first character in the
message.
mesg[0] == *mesg ; // the 0th element of an array is the same
as dereferencing mesg as a pointer.
Pointer variables (of the appropriate type) can be assigned an
array identifier:
char *p =
mesg ; // *p == mesg[0] == *mesg == 'H'
Pointers can be indexed like arrays (rarely done but
useful at times):
ch = p[4]
;
// ch == 'o'
Pointers can have integers added or subtracted to
generate new pointers:
char *q ;
q = p + 4 ; // q points to the character 4 past p (here, *q ==
'o')
Pointer difference gives the "distance" in underlying type
units between two pointers:
int dist ;
dist = q - p ; // dist == 4
Combining dereferencing and increment/decrement:
p
= mesg ;
ch = *p++ ; // ch == 'H' and p ==
&mesg[1]
q = mesg + 4 ;
ch = *q-- ; // ch == 'o' and q ==
&mesg[3]
p = mesg ;
ch = *++p ; // ch == 'e' and p ==
&mesg[1]
q = mesg + 4 ;
ch = *--q ;
// ch == 'l' and q == &mesg[3]
For this activity you will implement three versions of a basic find
function to find information in a string; in two of the three cases you
will implement both an array version with indexing and a pointer version
using pointer arithmetic.
Create a directory Find at
the top level of your git repository.Download the file find.zip
into this directory, extract
the archive contents, and remove the zip file:
unzip
find.zip
rm find.zip
Make the executable test_find (the default target of the Makefile)
and execute the result and use valgrind to do a quality check :
make
./test_find
valgrind ./test_find
At this point, use git to add, commit, and push the skeleton. This will show that you at least were able to initialize the project.
File find.h declares the interfaces for five variants of a "find" function using both character arrays and character pointers, file find.c contains skeleton implementations of these functions, and test_find.c contains unit tests for the functions (you may assume any string arguments to the functions are properly terminated):
As always, complete the Activity Journal, including your estimated time, your plan, your actual time, and observations.
We will pull your repository after the due date; the assessment is in
terms of this pulled information. We will compile and link your program
using the simple command:
make
If this does not create a program test_find without
syntax or linking errors you cannot receive a grade above 35, and your
grade will almost certainly be less than this.