© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,

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.
2007 Pearson Education, Inc. All rights reserved 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.
CSC141- Introduction to Computer Programming
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
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
© 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.
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.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
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.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
 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,
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
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 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
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.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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
Chapter: 7-12 Final exam review.
C Structures, Unions, Bit Manipulations and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing Members of Structures 10.5Using Structures with Functions 10.6 typedef 10.7Example: High-Performance Card Shuffling and Dealing Simulation 10.8Unions 10.9Bitwise Operators 10.10Bit Fields 10.11Enumeration Constants

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn: –To be able to create and use structures, unions and enumerations. –To be able to pass structures to functions call by value and call by reference. –To be able to manipulate data with the bitwise operators. –To be able to create bit fields for storing data compactly.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 define structure variables Definitions –Defined like other variables: card oneCard, deck[ 52 ], *cPtr; –Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Structure Definitions

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Initializing Structures Initializer lists –Example: card oneCard = { "Three", "Hearts" }; Assignment statements –Example: card threeHearts = oneCard; –Could also define and initialize threeHearts as follows: card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 fig10_02.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 fig10_02.c (Part 2 of 2) Program Output Ace of Spades

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 fig10_03.c (Part 1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 fig10_03.c (Part 2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 fig10_03.c (3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 fig10_03.c (4 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 Program Output Four of Clubs Three of Hearts Three of Diamonds Three of Spades Four of Diamonds Ace of Diamonds Nine of Hearts Ten of Clubs Three of Clubs Four of Hearts Eight of Clubs Nine of Diamonds Deuce of Clubs Queen of Clubs Seven of Clubs Jack of Spades Ace of Clubs Five of Diamonds Ace of Spades Five of Clubs Seven of Diamonds Six of Spades Eight of Spades Queen of Hearts Five of Spades Deuce of Diamonds Queen of Spades Six of Hearts Queen of Diamonds Seven of Hearts Jack of Diamonds Nine of Spades Eight of Hearts Five of Hearts King of Spades Six of Clubs Eight of Diamonds Ten of Spades Ace of Hearts King of Hearts Four of Spades Jack of Hearts Deuce of Hearts Jack of Clubs Deuce of Spades Ten of Diamonds Seven of Spades Nine of Clubs King of Clubs Six of Diamonds Ten of Hearts King of Diamonds

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Unions union –Memory that contains a variety of objects over time –Only contains one data member at a time –Members of a union share space –Conserves storage –Only the last data member defined can be accessed union definitions –Same as struct union Number { int x; float y; }; union Number value;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Unions Valid union operations –Assignment to union of same type: = –Taking address: & –Accessing union members:. –Accessing members using pointers: ->

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 fig10_05.c (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 fig10_05.c (2 of 2) Put a value in the integer member and print both members. int: 100 double: Put a value in the floating member and print both members. int: 0 double:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators All data represented internally as sequences of bits –Each bit can be either 0 or 1 –Sequence of 8 bits forms a byte

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 fig10_07.c (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 fig10_07.c (2 of 2) Enter an unsigned integer: =

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 fig10_09.c (1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 fig10_09.c (2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 fig10_09.c (3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 fig10_09.c (4 of 4) Program Output The result of combining the following = = using the bitwise AND operator & is 1 = The result of combining the following 15 = = using the bitwise inclusive OR operator | is 255 = The result of combining the following 139 = = using the bitwise exclusive OR operator ^ is 76 = The one's complement of = is =

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 fig10_13.c (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 fig10_13.c (2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 Program Output The result of left shifting 960 = bit positions using the left shift operator << is = The result of right shifting 960 = bit positions using the right shift operator >> is 3 =

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bitwise Operators

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bit Fields Bit field –Member of a structure whose size (in bits) has been specified –Enable better memory utilization –Must be defined as int or unsigned –Cannot access individual bits Defining bit fields –Follow unsigned or int member with a colon ( : ) and an integer constant representing the width of the field –Example: struct BitCard { unsigned face : 4; unsigned suit : 2; unsigned color : 1; };

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Bit Fields Unnamed bit field –Field used as padding in the structure –Nothing may be stored in the bits struct Example { unsigned a : 13; unsigned : 3; unsigned b : 4; } –Unnamed bit field with zero width aligns next bit field to a new storage unit boundary

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 fig10_16.c (1 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 fig10_16.c (2 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 fig10_16.c (3 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 Program Output Card: 0 Suit: 0 Color: 0 Card: 0 Suit: 2 Color: 1 Card: 1 Suit: 0 Color: 0 Card: 1 Suit: 2 Color: 1 Card: 2 Suit: 0 Color: 0 Card: 2 Suit: 2 Color: 1 Card: 3 Suit: 0 Color: 0 Card: 3 Suit: 2 Color: 1 Card: 4 Suit: 0 Color: 0 Card: 4 Suit: 2 Color: 1 Card: 5 Suit: 0 Color: 0 Card: 5 Suit: 2 Color: 1 Card: 6 Suit: 0 Color: 0 Card: 6 Suit: 2 Color: 1 Card: 7 Suit: 0 Color: 0 Card: 7 Suit: 2 Color: 1 Card: 8 Suit: 0 Color: 0 Card: 8 Suit: 2 Color: 1 Card: 9 Suit: 0 Color: 0 Card: 9 Suit: 2 Color: 1 Card: 10 Suit: 0 Color: 0 Card: 10 Suit: 2 Color: 1 Card: 11 Suit: 0 Color: 0 Card: 11 Suit: 2 Color: 1 Card: 12 Suit: 0 Color: 0 Card: 12 Suit: 2 Color: 1 Card: 0 Suit: 1 Color: 0 Card: 0 Suit: 3 Color: 1 Card: 1 Suit: 1 Color: 0 Card: 1 Suit: 3 Color: 1 Card: 2 Suit: 1 Color: 0 Card: 2 Suit: 3 Color: 1 Card: 3 Suit: 1 Color: 0 Card: 3 Suit: 3 Color: 1 Card: 4 Suit: 1 Color: 0 Card: 4 Suit: 3 Color: 1 Card: 5 Suit: 1 Color: 0 Card: 5 Suit: 3 Color: 1 Card: 6 Suit: 1 Color: 0 Card: 6 Suit: 3 Color: 1 Card: 7 Suit: 1 Color: 0 Card: 7 Suit: 3 Color: 1 Card: 8 Suit: 1 Color: 0 Card: 8 Suit: 3 Color: 1 Card: 9 Suit: 1 Color: 0 Card: 9 Suit: 3 Color: 1 Card: 10 Suit: 1 Color: 0 Card: 10 Suit: 3 Color: 1 Card: 11 Suit: 1 Color: 0 Card: 11 Suit: 3 Color: 1 Card: 12 Suit: 1 Color: 0 Card: 12 Suit: 3 Color: 1

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Enumeration Constants Enumeration –Set of integer constants represented by identifiers –Enumeration constants are like symbolic constants whose values are automatically set Values start at 0 and are incremented by 1 Values can be set explicitly with = Need unique constant names –Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Creates a new type enum Months in which the identifiers are set to the integers 1 to 12 –Enumeration variables can only assume their enumeration constant values (not the integer representations)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 fig10_18.c

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 Program Output 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December