2007 Pearson Education, Inc. All rights reserved. 1 10 C Structures, Unions, Bit Manipulations and Enumerations.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
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.
Break Time Remaining 10:00.
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Bright Futures Guidelines Priorities and Screening Tables
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.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
2007 Pearson Education, Inc. All rights reserved 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.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Adding Up In Chunks.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Clock will move after 1 minute
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Data Structures Using C++ 2E
 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.
© 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.
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
 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.
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Presentation transcript:

2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations

2007 Pearson Education, Inc. All rights reserved Introduction 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

2007 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

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

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

2007 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

2007 Pearson Education, Inc. All rights reserved. 7 Outline fig10_02.c (1 of 2 ) Structure definition Structure definition must end with semicolon Dot operator accesses members of a structure

2007 Pearson Education, Inc. All rights reserved. 8 Outline fig10_02.c (2 of 2 ) Arrow operator accesses members of a structure pointer

2007 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 – Pass a pointer to the structure 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

2007 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

2007 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

2007 Pearson Education, Inc. All rights reserved. 12 Outline fig10_03.c (1 of 3 ) Each card has a face and a suit Card is now an alias for struct card

2007 Pearson Education, Inc. All rights reserved. 13 Outline fig10_03.c (2 of 3 ) Constant pointer to modifiable array of Card s Fills the deck by giving each Card a face and suit

2007 Pearson Education, Inc. All rights reserved. 14 Outline fig10_03.c (3 of 3 ) Each card is swapped with another, random card, shuffling the deck

2007 Pearson Education, Inc. All rights reserved. 15 Outline

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

2007 Pearson Education, Inc. All rights reserved. 17 Outline fig10_05.c (1 of 2 ) Union definition Union definition must end with semicolon Note that y has no value

2007 Pearson Education, Inc. All rights reserved. 18 Outline fig10_05.c (2 of 2 ) Giving y a value removes x s value

2007 Pearson Education, Inc. All rights reserved Bitwise Operators All data is represented internally as sequences of bits – Each bit can be either 0 or 1 – Sequence of 8 bits forms a byte Bitwise data manipulations are machine dependent.

2007 Pearson Education, Inc. All rights reserved. 20 Fig | Bitwise operators.

2007 Pearson Education, Inc. All rights reserved. 21 Outline fig10_07.c (1 of 2 )

2007 Pearson Education, Inc. All rights reserved. 22 Outline fig10_07.c (2 of 2 ) displayMask is a 1 followed by 31 zeros Bitwise AND returns nonzero if the leftmost bits of displayMask and value are both 1, since all other bits in displayMask are 0s.

2007 Pearson Education, Inc. All rights reserved. 23 Common Programming Error Using the logical AND operator ( && ) for the bitwise AND operator ( & ) and vice versa is an error.

2007 Pearson Education, Inc. All rights reserved. 24 Fig | Results of combining two bits with the bitwise AND operator &.

2007 Pearson Education, Inc. All rights reserved. 25 Fig | Results of combining two bits with the bitwise inclusive OR operator |.

2007 Pearson Education, Inc. All rights reserved. 26 Fig | Results of combining two bits with the bitwise exclusive OR operator ^.

2007 Pearson Education, Inc. All rights reserved. 27 Outline fig10_09.c (1 of 3 ) Bitwise AND sets each bit in the result to 1 if the corresponding bits in the operands are both 1

2007 Pearson Education, Inc. All rights reserved. 28 Outline fig10_09.c (2 of 3 ) Bitwise inclusive OR sets each bit in the result to 1 if at least one of the corresponding bits in the operands is 1 Bitwise exclusive OR sets each bit in the result to 1 if only one of the corresponding bits in the operands is 1 Complement operator sets each bit in the result to 0 if the corresponding bit in the operand is 1 and vice versa

2007 Pearson Education, Inc. All rights reserved. 29 Outline fig10_09.c (3 of 3 )

2007 Pearson Education, Inc. All rights reserved. 30 Outline fig10_10.c

2007 Pearson Education, Inc. All rights reserved. 31 Outline fig10_13.c (1 of 3 ) Left shift operator shifts all bits left a specified number of spaces, filling in zeros for the empty bits

2007 Pearson Education, Inc. All rights reserved. 32 Outline fig10_13.c (2 of 3 ) Right shift operator shifts all bits right a specified number of spaces, filling in the empty bits in an implementation-defined manner

2007 Pearson Education, Inc. All rights reserved. 33 Outline fig10_13.c (3 of 3 )

2007 Pearson Education, Inc. All rights reserved. 34 Fig | The bitwise assignment operators.

2007 Pearson Education, Inc. All rights reserved. 35 Fig | Operator precedence and associativity. (Part 1 of 2.)

2007 Pearson Education, Inc. All rights reserved. 36 Fig | Operator precedence and associativity. (Part 2 of 2.)

2007 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; };

2007 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

2007 Pearson Education, Inc. All rights reserved. 39 Performance Tip 10.3 Bit fields help conserve storage.

2007 Pearson Education, Inc. All rights reserved. 40 Outline fig10_16.c (1 of 2 ) Bit fields determine how much memory each member of a structure can take up

2007 Pearson Education, Inc. All rights reserved. 41 Outline fig10_16.c (2 of 2 )

2007 Pearson Education, Inc. All rights reserved. 42 Outline

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

2007 Pearson Education, Inc. All rights reserved. 44 Outline fig10_18.c (1 of 2 ) Enumeration sets the value of constant JAN to 1 and the following constants to 2, 3, 4…

2007 Pearson Education, Inc. All rights reserved. 45 Outline fig10_18.c (2 of 2 ) Like symbolic constants, enumeration constants are replaced by their values at compile time