09-arg-templates.student - Today Arguments to programs argc...

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

View Full Document Right Arrow Icon
Today: * Arguments to programs: argc and argv * File I/O * Templates---generalizing across types 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[] So, in our example above, the argv array would look like this: +-+ +---+ +---+---+---+---+---+---+---+---+---+---+
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
argv |---->| ------>| s | u | b | m | i | t | 2 | 8 | 0 |\0 | +-+ +---+ +---+---+---+---+---+---+---+---+---+---+ | | +---+---+ | ------>| 2 |\0 | +---+ +---+---+ | | +---+---+---+---+---+---+---+ | ------>| p | 2 | . | c | p | p |\0 | +---+ +---+---+---+---+---+---+---+ | ---- +---+ \ | ----- --- - (Note that the last entry is a pointer to NULL. This is guaranteed.) 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. Notice that there is another way: the NULL pointer at the end of the
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 ]}

Page1 / 8

09-arg-templates.student - Today Arguments to programs argc...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online