{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

09-arg-templates.student - Last Time Constants Enumerations...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Time: * Constants * Enumerations * Structures Today: * Arguments to programs: argc and argv * Templates---generalizing across types * File I/O So far, we haven't really considered how our programs can interact with "the outside world." For example, consider using the submit280 program to submit your project 2 solution: submit280 2 p2.cpp The first word, "submit280" is the name of the program to run. The second and third words are *arguments* to the submit280 program; they are passed to submit280 for its consideration, much as arguments are passed to functions. The *shell* (the program to which you type commands) collects arguments and passes them to the programs it executes. Unlike functions, programs have no specific "type signature" of arguments they expect, and it is up to them to interpret what they are passed correctly. We'll spend the rest of today discussing the mechanism C++ uses to pass arguments. The first challenge is that a program can't know in advance how many arguments it will be passed. So, it has to be written to be generic with respect to number of arguments. However, we've already seen one type which enables us to pass an argument of unknown size to a function. What is it? Answer: arrays. Remember the function to sum up the elements of an array? It's type signature was: int sum(int a[], unsigned int size); It took as an argument both the array and its size---the number of elements in that array. Likewise, arguments are passed to programs as an array too. Since each argument is just a sequence of characters, this array is an array of C-strings. Remember, a C-string is itself an array of char, so what we have is an array of arrays. Since an array-of-char can itself be thought of as a pointer-to-char, it's common to declare an array-of-C-strings as: char *argv[] Reading from inside out: argv is an array of pointers to char. The name "argv" is used by convention: it is short for "argument vector"
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
or "argument values". So, in our example above, the argv array would look like this: +-+ +---+ +---+---+---+---+---+---+---+---+---+---+ argv |---->| ------>| s | u | b | m | i | t | 2 | 8 | 0 |\0 | +-+ +---+ +---+---+---+---+---+---+---+---+---+---+ | | +---+---+ | ------>| 2 |\0 | +---+ +---+---+ | | +---+---+---+---+---+---+---+ | ------>| p | 2 | . | c | p | p |\0 | +---+ +---+---+---+---+---+---+---+ | ---- +---+ \ | ----- --- - Notice that the last entry is a pointer to NULL. NULL is the special pointer that "points to nothing", and it is bitwise zero. This is guaranteed to be the last entry in argv. Notice also that argv[0] is the name of the program being executed. This is because it is possible for the same program to be given different names, and do different things depending on what name it was called with. In addition to the array of strings itself, we need some way of knowing how many strings are in the array. There are actually two ways of doing that. First the program is also passed the number explicitly: int argc where argc is "the number of strings in the argv array." So, in our submit280 example, argc would be 3: one for the program name, and two for the two arguments. Again, the name argc is used by convention: it stands for "argument count".
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}