Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu CSCI 240 Review for Project 1

2 Dale Roberts 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"

3 Dale Roberts 1. Initialize variables 2. Function calls ( fopen ) 2.1 Specify open type (read or write) 3. Copy file 1/* Fig. 14.3: fig14_03.c 2 Using command-line arguments */ 3#include 4 5int main( int argc, char *argv[] ) 6{6{ 7 FILE *inFilePtr, *outFilePtr; 8 int c; 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} Notice argc and argv[] in main argv[1] is the second argument, and is being read. argv[2] is the third argument, and is being written to. Loop until End Of File. fgetc a character from inFilePtr and fputc it into outFilePtr.

4 Dale Roberts 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

5 Dale Roberts 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

6 Dale Roberts 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

7 Dale Roberts Structure Definitions Example struct card { char *face; char *face; char *suit; }; 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

8 Dale Roberts 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 *face; char *suit; char *suit; } oneCard, deck[ 52 ], *cPtr;

9 Dale Roberts 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

10 Dale Roberts 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

11 Dale Roberts 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

12 Dale Roberts 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

13 Dale Roberts 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.

14 Dale Roberts ADT Example Let’s consider an ADT for a Histrogram. What might its operations be? Histogram *h; h = create_histogram(); increment_count(h, key); key_count(h) max_frequency(h) is_new_key(h, key); key = get_least_key(h); key = get_next_key(h, current_key); get_least_freq(h); get_next_freq(h); destroy_hisogram(h);

15 Dale Roberts 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!


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"

Similar presentations


Ads by Google