main
function as an array of strings. For example, you learned that you could write a program
int main(int argc, char * argv[]) { ...}and invoke the executable using the command line
./a.out one two threeIn this case, the value
4
will be passed to argc
in the main function, and
the string array argv
will be allocate memory space as in the diagram below.
argv
array depicted above. You will be using this library in future assignments, so please make sure that your code is correct and clean.
You may want to review C strings and dynamic memory allocation before starting on this assignment.
void InitArray(char *array[], int n); /* Input: @param array is an array of uninitialized pointers * @param n is the number of entries in the array * Action: Initialize all entries in the array to NULL */Note: The function
InitArray
assumes that no strings exist in the array at the time it is called.
void Insert(char *array[], char * s, int pos); /* Input: @param array is an array of strings * @param s is a string to be inserted in the array * @param pos is a valid array index * Action: Insert s in position pos in the array */Note: You will need to use malloc to dynamically allocate memory for the string to be inserted in the array, and strcpy to copy the string into the newly allocated memory.
void Remove(char *array[], int pos); /* Input: @param array is an array of strings * @param pos is a valid array index * Action: Remove the string from position pos in the array (if any) */Note: If the entry value is NULL,
Remove
does not do anything. Otherwise, Remove
invokes free to deallocate the memory space allocated for the string with malloc
,
so that it does not create any memory leaks. Reset the array entry to NULL
to mark that
no strings are attached to it.
void RemoveAll(char *array[], int n); /* Input: @param array is an array of strings * @param n is number of entries in the array * Action: Remove all existing strings from the array */Note:
RemoveAll
may invoke Remove
to do the job.
void Print(char *array[], int pos); /* Input: @param array is an array of strings * @param pos is a valid array index * Action: Print out the string at position pos (if any) in the array */Note:
Print
must check first if the array entry is NULL (otherwise the printf
function will crash
if invoked with a NULL
pointer as an argument).
void PrintAll(char *array[], int n); /* Input: @param array is an array of strings * @param n is the number of entries in the array * Action: Print out all strings in the array (i.e., all non-NULL array entries) */
Code | Memory snapshot of myarray |
#define MAX 8 char * myarray[MAX]; | |
InitArray(myarray, MAX); | |
Insert(myarray, "one", 0); | |
Insert(myarray, "two", 1); | |
Insert(myarray, "three",2); | |
Remove(myarray, 1); | |
PrintAll(myarray, MAX); | Output is: one three |
A sample driver is provided. Feel free to modify it, but make sure that it thoroughly tests the correctness of each function.
To compile your code, use must supply all source (not header) files to
the gcc complier:
gcc stringarray.c driver.c -o driver
The result is an executable called driver.
Alternately, you may use this Makefile and simply type in make at the shell prompt to compile your code. This saves you from typing in a long command line every time you need to compile your code. Makefiles are special format files that help build and manage projects composed of multiple modules. The key point to remember is that you need to insert a TAB character (not spaces) before each command in the Makefile. If interested, you may read more about using make and writing Makefiles.
stringarray
directory create a text file named readme
(not readme.txt
, or README
, or Readme
, etc.)
that contains:
Turn in a printout copy of the readme
file, and each of the three files driver.c, stringarray.c and stringarray.h (one printout per group). You will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.
Leave the source code for all exercises in your directory systems/stringarray. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory stringarray using the following Unix command:
cp -r ~/systems/stringarray ~/systems/stringarray-copyA new directory called stringarray-copy will be created in your systems directory. You may now make any changes you want to the files in your stringarray-copy directory, at any time.
No credit will be given for code that does not compile. | |
100 | Total points possible |
20 | Program compiles and runs without crashing |
10 | Driver includes all test cases (for both null and non-null entries) |
60 | Each function performs its specified action |
10 | Your readme file is present |