09-arg-templates.student

09-arg-templates.student - Last Time: * Constants *...

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 DocumentRight 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
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.

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 8

09-arg-templates.student - Last Time: * Constants *...

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