09+-+Argv%2C+IO%2C+and+Templates

09+-+Argv%2C+IO%2C+and+Templates - 9/1/10 Intro to I/O The...

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

View Full Document Right Arrow Icon
9/1/10 1 Input and Output EECS 280 Programming and Introductory Data Structures Intro to I/O The basics So far, we've considered programs that always do exactly the same thing. We haven't really examined I/O in great detail nor have we tried passing arguments to the programs we are running. For example, consider using the submit280 program from EECS 280 at UM to submit your project 2 solution: submit280 2 p2.cpp Intro to I/O The basics 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. These arguments 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. Intro to I/O The basics Unlike functions, programs have no specific “type signature” for the arguments they expect and it’s up to the program to interpret what it is passed correctly. 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. We’ve already seen one type which enables us to pass an argument of unknown size to a function. Question : What is it? Intro to I/O The basics Unlike functions, programs have no specific “type signature” for the arguments they expect and it’s up to the program to interpret what it is passed correctly. 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. We’ve already seen one type which enables us to pass an argument of unknown size to a function. Question : What is it? Answer : Arrays Intro to I/O The basics Recall the function to sum up the elements of an array. int sum(int a[], unsigned int size); It took two arguments as input: 1. The array 2. The array’s size (i.e. the number of elements in the array). 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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
9/1/10 2 Input/Output argv 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 and it is short for “argument vector” or “argument values”. Input/Output argv Pictorially, this would look like the following in memory: s u b m i t 2 8 0 \0 2 \0 p 2 . c p p \0 argv submit280 2 p2.cpp char *argv[] Input/Output argv Pictorially, this would look like the following in memory: s u b m i t 2 8 0 \0 2 \0 p 2 . c p p \0 argv submit280 2 p2.cpp Note : argv[0] is the name of the program being executed. This is
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 / 6

09+-+Argv%2C+IO%2C+and+Templates - 9/1/10 Intro to I/O The...

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