09+-+Argv,+IO,+and+Templates

09+-+Argv,+IO,+and+Templates - EECS 280 Programming and...

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

View Full Document Right Arrow Icon
Input and Output EECS 280 Programming and Introductory Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
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:
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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).
Background image of page 6
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 .
Background image of page 7

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

View Full DocumentRight Arrow Icon
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[]
Background image of page 8
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 because it is possible for the same program to be given different names, and do char *argv[]
Background image of page 9

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

View Full DocumentRight Arrow Icon
Input/Output argc In addition to the array of strings itself, we
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 33

09+-+Argv,+IO,+and+Templates - EECS 280 Programming and...

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

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