1 EPSII 59:006 Spring 2004. 2 HW’s and Solutions on WebCT.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

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.
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
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 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,
Programming Languages -1 (Introduction to C) structures Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 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,
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Dale Roberts, Lecturer Computer Science, IUPUI.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
1 CSC 211 Data Structures Lecture 5 Dr. Iftikhar Azim Niaz 1.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
 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.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
User Defined Data Types
Chapter 7 - Pointers Outline 7.1 Introduction
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 7 - Pointers Outline 7.1 Introduction
C Structures, Unions, Bit Manipulations and Enumerations
File Processing (Cont.) and Structures
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Introduction to Programming
Chapter: 7-12 Final exam review.
C Structures, Unions, Bit Manipulations and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

1 EPSII 59:006 Spring 2004

2 HW’s and Solutions on WebCT

3 Introduction 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

4 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

5 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, but using the struct keyword: struct card oneCard, deck[ 52 ], *cPtr;  Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;

6 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

7 Initializing Structures Initializer lists  Example: struct card oneCard = { "Three", "Hearts" }; Assignment statements  Example: struct card threeHearts = oneCard;  Could also declare and initialize threeHearts as follows: struct card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”;

8 Accessing Members of Structures Accessing structure members (put on board)  Dot operator (. ) used with structure variables struct card myCard = { "Three", "Hearts" }; printf( "%s", myCard.suit );  Arrow operator ( -> ) used with pointers to structure variables struct card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit );  myCardPtr->suit is equivalent to ( *myCardPtr ).suit

9 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

10 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 Syntactically equivalent to a new data type, but only for the program where you use it

11 Example: High-Performance Card- shuffling and Dealing Simulation Pseudocode:  Create an array of card structures  Put cards in the deck  Shuffle the deck  Deal the cards

12 Load headers Define struct Function prototypes Initialize deck[] and face[] Initialize suit[] 1/* Fig. 10.3: fig10_03.c 2 The card shuffling and dealing program using structures */ 3#include 4#include 5#include 6 7struct card { 8 const char *face; 9 const char *suit; 10}; 11 12typedef struct card Card; 13 14void fillDeck( Card * const, const char *[], 15 const char *[] ); 16void shuffle( Card * const ); 17void deal( const Card * const ); 18 19int main() 20{ 21 Card deck[ 52 ]; 22 const char *face[] = { "Ace", "Deuce", "Three", 23 "Four", "Five", 24 "Six", "Seven", "Eight", 25 "Nine", "Ten", 26 "Jack", "Queen", "King"}; 27 const char *suit[] = { "Hearts", "Diamonds", 28 "Clubs", "Spades"}; srand( time( NULL ) );

13 fillDeck Shuffle deal Function Definitions fillDeck( deck, face, suit ); 33 shuffle( deck ); 34 deal( deck ); 35 return 0; 36} 37 38void fillDeck( Card * const wDeck, const char * wFace[], 39 const char * wSuit[] ) 40{ 41 int i; for ( i = 0; i <= 51; i++ ) { 44 wDeck[ i ].face = wFace[ i % 13 ]; 45 wDeck[ i ].suit = wSuit[ i / 13 ]; 46 } 47} 48 49void shuffle( Card * const wDeck ) 50{ 51 int i, j; 52 Card temp; for ( i = 0; i <= 51; i++ ) { 55 j = rand() % 52; 56 temp = wDeck[ i ]; 57 wDeck[ i ] = wDeck[ j ]; 58 wDeck[ j ] = temp; 59 } 60} Put all 52 cards in the deck. face and suit determined by remainder (modulus). Select random number between 0 and 51. Swap element i with that element.

14 Function Definitions 61 62void deal( const Card * const wDeck ) 63{ 64 int i; for ( i = 0; i <= 51; i++ ) 67 printf( "%5s of %-8s%c", wDeck[ i ].face, 68 wDeck[ i ].suit, 69 ( i + 1 ) % 2 ? '\t' : '\n' ); 70} Cycle through array and print out data.

15 Program Output Eight of Diamonds Ace of Hearts Eight of Clubs Five of Spades Seven of Hearts Deuce of Diamonds Ace of Clubs Ten of Diamonds Deuce of Spades Six of Diamonds Seven of Spades Deuce of Clubs Jack of Clubs Ten of Spades King of Hearts Jack of Diamonds Three of Hearts Three of Diamonds Three of Clubs Nine of Clubs Ten of Hearts Deuce of Hearts Ten of Clubs Seven of Diamonds Six of Clubs Queen of Spades Six of Hearts Three of Spades Nine of Diamonds Ace of Diamonds Jack of Spades Five of Clubs King of Diamonds Seven of Clubs Nine of Spades Four of Hearts Six of Spades Eight of Spades Queen of Diamonds Five of Diamonds Ace of Spades Nine of Hearts King of Clubs Five of Hearts King of Spades Four of Diamonds Queen of Hearts Eight of Hearts Four of Spades Jack of Hearts Four of Clubs Queen of Clubs

16 Enumerations User-defined data type Provide symbolic names for a range of integer values Primary function is to improve program readability

17 Enumerations enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; Example: // sets JAN to 0, FEB to 1, …, DEC to 11) enum months month. for (month = JAN; month < =DEC; month ++) { … }

18 /* Fig : fig10_18.c Using an enumeration type */ #include /* enumeration constants represent months of the year */ enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; int main() { enum months month; /* can contain any of the 12 months */ /* initialize array of pointers */ const char *monthName[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /* loop through months */ for ( month = JAN; month <= DEC; month++ ) { printf( "%2d%11s\n", month, monthName[ month ] ); } /* end for */ return 0; /* indicates successful termination */ } /* end main */ Enumeration Example Note that the enumeration starts at 1 rather than 0.

19 Output./a.out 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December

20 Why Jan=1 ??? Just to illustrate that it can be done

21 Enumerations—Some Caveats Variables declared to be of an enum type are really just integers enum variables are not range checked—e.g consider the following (nonsense) example: #include main() { enum months {JAN, FEB, MAR}; enum months mon; int i; mon = JAN; mon =13; i = FEB; printf("mon = %d\n i = %d\n", mon, i); } mon = 13 i = 1 Note: no range checking Can assign an enum constant to an integer variable.

22 Struct Example: Record int age struct record *next char name[10] struct Record 1 struct record *next Bob 5 struct record *next Tom 8 struct record *next Fred 0

23 struct Record { int age; // just a plain old int struct Record *next; // a pointer to another Record char name[10]; // plain old string }; main() { int i = 0; struct Record *p=0; // pointer to a Record struct Record recs[100]; recs[0].age=1; recs[0].next=0; strcpy(recs[0].name,"Bob"); // what is wrong with // recs[0].name = "Bob"; recs[1].age=5; recs[1].next=0; strcpy(recs[1].name,"Tom"); recs[2].age=8; recs[2].next=0; strcpy(recs[2].name,"Fred"); for(i=0;i<=2;i++) { printf("age = %d name %s\n", recs[i].age, recs[i].name); } // Establishing links recs[0].next = &recs[1]; recs[1].next = &recs[2]; // another way to do loop p = &recs[0]; // set pointer to first Record while(p) { printf("age = %d name = %s\n",p->age,p- >name); p = p->next; }

24 /* example showing structures and rudimentary linked list */ /* note, without dynamic memory allocation, linked list is */ /* problematic */ // Define a new "type" called "Record" -- analogous to "int" // Record has 3 data types within it // struct Record { int age; // just a plain old int struct Record *next; // a ptr to another Record char name[10]; // plain old string }; main() { struct Record *p=0; // pointer to a Record struct Record first = {1,0,"Bob"}; // first is a Record struct Record second = {5,0,"Tom"}; struct Record third = {8,0,"Fred"}; first.next = &second; // Establishing links second.next = &third; p = &first; // set pointer to first Record while(p) { printf("age = %d name = %s\n",p->age,p->name); p = p->next; }