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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Guide To UNIX Using Linux Third Edition
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Lesson #8 Structures Linked Lists Command Line Arguments.
5.13 Recursion Recursive functions Functions that call themselves
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
TMF1414 Introduction to Programming
C Structures, Unions, Bit Manipulations and Enumerations
File Processing (Cont.) and Structures
Functions Declarations CSCI 230
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 14 - Advanced C Topics
Dale Roberts, Lecturer IUPUI
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
C Structures, Unions, Bit Manipulations and Enumerations
Data Structures and Algorithms Introduction to Pointers
Arrays.
Dale Roberts, Lecturer IUPUI
Characters and Strings Functions
C Language B. DHIVYA 17PCA140 II MCA.
Characters and Strings
Abstract Data Types Stacks CSCI 240
Structures Declarations CSCI 230
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI CSCI 240 Review for Project 1

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"

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 ) if ( ( outFilePtr = fopen( argv[ 2 ], "w" ) ) != NULL ) while ( ( c = fgetc( inFilePtr ) ) != EOF ) 18 fputc( c, outFilePtr ); else 21 printf( "File \"%s\" could not be opened\n", argv[ 2 ] ); else 24 printf( "File \"%s\" could not be opened\n", argv[ 1 ] ); 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.

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

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

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

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

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;

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

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

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

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

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.

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);

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!