09-arg-templates.student

09-arg-templates.student - Today: * Arguments to programs:...

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 DocumentRight Arrow Icon
+---+ +---+---+---+---+---+---+---+---+---+---+ argv ---->| ------>| s | u | b | m | i | t | 2 | 8 | 0 |\0 | +---+ +---+---+---+---+---+---+---+---+---+---+ | | +---+---+ | ------>| 2 |\0 | +---+ +---+---+ | | +---+---+---+---+---+---+---+ | ------>| p | 2 | . | c | p | p |\0 | +---+ +---+---+---+---+---+---+---+ In addition to the array of strings itself, we need some way of knowing how many strings are in the array, so the program is also passed int argc A program that accepts arguments has a slightly different type signature for main than simple programs: int main(int argc, char *argv[]) Suppose we wanted to write a program that is given a list of integers as it's arguments, and prints out the sum of that list. Before we can write this program we need a way to convert from
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 01/28/2010 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 7

09-arg-templates.student - Today: * Arguments to programs:...

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