150 likes | 268 Views
Department of Computer and Information Science, School of Science, IUPUI. CSCI 240. Review for Project 1. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Using Command-Line Arguments. Pass arguments to main on DOS or UNIX Define main as
E N D
Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Review for Project 1 Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
Using Command-Line Arguments • Pass arguments to main on DOS or UNIX • Define main as • int main( int argc, char *argv[] ) • int argc • Number of arguments passed • char *argv[] • Array of strings • Has names of arguments in order • argv[ 0 ] is first argument • Example: $ copy input output • argc: 3 • argv[ 0 ]: "copy" • argv[ 1 ]: "input" • argv[ 2 ]: "output"
1 /* Fig. 14.3: fig14_03.c Notice argc and argv[] in main argv[2] is the third argument, and is being written to. 2 Using command-line arguments */ 3 #include <stdio.h> argv[1] is the second argument, and is being read. 4 5 int main( int argc, char *argv[] ) 6 { 7 FILE *inFilePtr, *outFilePtr; 8 int c; Loop until End Of File. fgetc a character from inFilePtr and fputc it into outFilePtr. 9 10 if ( argc != 3 ) 11 printf( "Usage: copy infile outfile\n" ); 12 else 13 if ( ( inFilePtr = fopen( argv[ 1 ], "r" ) ) != NULL ) 14 15 if ( ( outFilePtr = fopen( argv[ 2 ], "w" ) ) != NULL ) 16 17 while ( ( c = fgetc( inFilePtr ) ) != EOF ) 18 fputc( c, outFilePtr ); 19 20 else 21 printf( "File \"%s\" could not be opened\n", argv[ 2 ] ); 22 23 else 24 printf( "File \"%s\" could not be opened\n", argv[ 1 ] ); 25 26 return 0; 27 } 1. Initialize variables 2. Function calls (fopen) 2.1 Specify open type (read or write) 3. Copy file
Notes on Compiling Multiple-Source-File Programs • Programs with multiple source files • Function definition must be in one file (cannot be split up) • Global variables accessible to functions in same file • Global variables must be defined in every file in which they are used • Example: • If integer myGlobal is defined in one file • To use it in another file you must include the statement extern int myGlobal; • extern • States that the variable is defined in another file • Function prototypes can be used in other files without an extern statement • Have a prototype in each file that uses the function
Notes on Compiling Multiple-Source-File Programs • Keyword static • Specifies that variables can only be used in the file in which they are defined • Programs with multiple source files • Tedious to compile everything if small changes have been made to only one file • Can recompile only the changed files • Procedure varies on system • UNIX: make utility
Structures • Structures • Collections of related variables (aggregates) under one name • Can contain variables of different data types • Commonly used to define records to be stored in files • Combined with pointers, can create linked lists, stacks, queues, and trees
Structure Definitions • Example struct card { char *face; char *suit; }; • struct introduces the definition for structure card • card is the structure name and is used to declare variables of the structure type • card contains two members of type char * • These members are face and suit
Structure Definitions • struct information • A struct cannot contain an instance of itself • Can contain a member that is a pointer to the same structure type • A structure definition does not reserve space in memory • Instead creates a new data type used to declare structure variables • Declarations • Declared like other variables: card oneCard, deck[ 52 ], *cPtr; • Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;
Structure Definitions • Valid Operations • Assigning a structure to a structure of the same type • Taking the address (&) of a structure • Accessing the members of a structure • Using the sizeof operator to determine the size of a structure
Accessing Members of Structures • Accessing structure members • Dot operator (.) used with structure variables card myCard; printf( "%s", myCard.suit ); • Arrow operator (->) used with pointers to structure variables card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); • myCardPtr->suit is equivalent to ( *myCardPtr ).suit
Using Structures With Functions • Passing structures to functions • Pass entire structure • Or, pass individual members • Both pass call by value • To pass structures call-by-reference • Pass its address • Pass reference to it • To pass arrays call-by-value • Create a structure with the array as a member • Pass the structure
typedef • typedef • Creates synonyms (aliases) for previously defined data types • Use typedef to create shorter type names • Example: typedef struct Card *CardPtr; • Defines a new type name CardPtr as a synonym for type struct Card * • typedef does not create a new data type • Only creates an alias
Abstract Data Types • A data type is a set of values and a collection of operations on those values. • An abstract data type is one that we create. • It has an interface that defines the data type and the operations that can act upon it. • It has an implementation of the functions declared in the interface. • Is is used by a client, who does not care about the implementation details.
ADT Example • Let’s consider an ADT for a Histrogram. What might its operations be? Histogram *h; is_new_key(h, key); h = create_histogram(); key = get_least_key(h); key = get_next_key(h, current_key); increment_count(h, key); key_count(h) get_least_freq(h); max_frequency(h) get_next_freq(h); destroy_hisogram(h);
ADT Example (cont) • The ADT should be hidden in its own file. The implementation details are encapsulated so that the client is unaware. • Suppose you get a better idea on how to implement histograms. Just change the implementation and recompile. No changed to the client are required!