C uses a mechanism which you will find reminiscent of java's command-line-argument mechanism (the "String[] args" argument to main()). There is an alternate allowable definition of main() in C and C++, whose declaration looks like this:
int main(int argc, char **argv)
"argc" stands for "argument count". It is the number of elements in "argv", "argument vector". (While "vector" and "array" mean different things in java, traditionally they are synonyms.)
While "argc" and "argv" are theoretically arbitrary parameter names, the use of the names "argc" and "argv" is so standard that you should never use any other name.
Since a string can be passed around as a value of type pointer-to-char, argv will be an array of pointers-to-char. And when argv is passed to main() by the operating system, it will decay into a pointer to its zeroth element, so the data will be of type pointer-to-pointer-to-char.
The array is one element larger than you would think. The program name itself is argv[0], so the first command-line argument is argv[1], and so on, and argc reflects the total number of items in the argv array including this argv[0]. Often we ignore argv[0] altogether, and you will undoubtedly want to ignore it for this assignment.
Example 1: Write a program which first checks that argc is at least 2, then prints the value of argv[1] (using %s). A session with this program might look like this, where '%' is your shell prompt:
% ./a.out hello, world hello, %Since "world" is argv[2], it didn't print that part. The shell (command interpreter) divides up the arguments based on spaces.
Solution:
#include <stdio.h> int main(int argc, char **argv) { if (argc >= 2) printf("%s\n", argv[1]); return 0; }We check that argc >= 2 before accessing argv[1], because it is illegal to exceed the bounds of an array in C, and it is undiagnosed... be careful!
Example 2: Write a program which prints all of the argv values in a loop. This is like the 'echo' command in unix.
Sample session (our program is not going to be quite as tidy as 'echo', as shown here):
% echo hello, world, how are you hello, world, how are you % ./a.out hello, world, how are you hello, world, how are you %
Answer:
#include <stdio.h> int main(int argc, char **argv) { int i; for (i = 1; i < argc; i++) printf("%s\n", argv[i]); return 0; }
Note the ignoring of argv[0], which would be "./a.out".
How would you use this argv stuff for assignment three?
Well, it is specified on the handout. You should expect four command-line arguments, which are the number of hosts of type 1, 2, and 3, respectively, and then the strategy number from 1 to 5.
You could do something like this. We expect argc to be 5, because it includes the argv[0] value of "./a.out" or whatever.
int main(int argc, char **argv) { if (argc != 5) { printf("usage: %s nspewers npeerpairs nclientserverpairs strategy\n", argv[0]); return 1; } if ((strategy = atoi(argv[4])) < 1 || strategy > 5) { printf("%s: strategy must be between 1 and 5 inclusive\n", argv[0]); return 1; } for (i = atoi(argv[1]); i > 0; i--) (new spewer)->schedule(); for (i = atoi(argv[2]); i > 0; i--) (new peerpair)->schedule(); for (i = atoi(argv[3]); i > 0; i--) (new clientserverpair)->schedule(); ...The stdlib function atoi() converts its string argument into an integer.
"strategy" is a hypothetical global variable here (you may prefer to call a function to set the strategy), and the above code assumes that each of your host subclasses has a member function "schedule()" which schedules the object's first event and puts it into the event queue. There's a good chance that you will change the above code, at least a little, perhaps substantially.