Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.

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.
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.
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.
© 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.
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.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
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 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.
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 *
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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:

Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University

Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing Members of Structures 10.5Using Structures with Functions 10.6Typedef 10.7Example: High-Performance Card Shuffling and Dealing Simulation 10.8Unions 10.9Bitwise Operators 10.10Bit Fields 10.11Enumeration Constants

10.1Introduction 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 (data structure, Chapter 12)

10.2 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 * - face and suit

Example: struct employee { char firstName[20]; char lastName[20]; int age; char gender; double hourlySalary; };

Example struct employee2 { char firstName[20]; char lastName[20]; int age; char gender; double hourlySalary; struct employee2 person; //ERROR struct employee2 *ePtr; //pointer };

10.2 Structure Definitions (II) Struct information  A struct cannot contain an instance of itself  Can contain a member that is a pointer to the same structure type  Structure definition does not reserve space in memory  Creates a new data type that used to declare structure variables.

Cont. Declarations  Declared like other variables: struct card oneCard, deck[ 52 ], *cPtr;  Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;

10.2 Structure Definitions (III) 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.3 Initializing Structures Initializer lists  Example: struct card { char *face; char *suit; }; struct card a = { "Three", "Hearts" } Assignment statements  Example: struct card threeHearts = a ;

Cont.  Or: struct card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”;

struct card { char *face; char *suit; };

10.4Accessing Members of Structures Accessing structure members  Dot operator (. ) - use with structure variable name struct card myCard; printf( "%s", myCard.suit );  Arrow operator ( -> ) - use with pointers to structure variables struct card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit );

Cont. myCardPtr->suit is equivalent to (*myCardPtr).suit

10.5 Using Structures With Functions Structure may be passed to functions  by passing individual structure members  by passing an entire structure or  by passing a pointer to a structure When structure or individual structure members are passed to a function  Call by value To pass a structure call be reference  Pass the address of the structure variable

Cont. Arrays of structures  Likes all other arrays  Automatically passed call by reference

10.6 Typedef typedef  Creates synonyms (aliases) for previously defined data types  Use typedef to create shorter type names.  Example: typedef struct card Card; Card deck [52];  Defines a new type name struct card as a synonym for type Card  typedef does not create a new data type Only creates an alias

10.7 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

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.

Cycle through array and print out data.

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

10.8Unions 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 declarations

Cont.  Same as struct union Number { int x; float y; };

Valid Initialization union number value = { 10 }; is a valid initialization of union variable value because the union is initialized with a int, but the following declaration would be invalid: union number value = { 1.43 };

10.8 Unions (II) Valid union operations  Assignment to union of same type: =  Taking address: &  Accessing union members:.  Accessing members using pointers: ->

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:

10.9 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

10.9 Bitwise Operators

MASK created with only one set bit i.e. ( ) The MASK is constantly AND ed with value. MASK only contains one bit, so if the AND returns true it means value must have that bit. value is then shifted to test the next bit.

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 =

Example:

10.10 Bit Fields Bit field  Member of a structure whose size (in bits) has been specified  Enable better memory utilization  Must be declared as int or unsigned  Cannot access individual bits Declaring bit fields

Cont.  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; };

10.10 Bit Fields (II) 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

Example:

10.11 Example: A Game of Chance and Introducing enum Enumeration  Set of integers represented by identifiers  Enumeration constants - like symbolic constants whose values automatically set Values start at 0 and are incremented by 1 Values can be set explicitly with = Need unique constant names  Declare variables as normal Enumeration variables can only assume their enumeration constant values (not the integer representations)

10.11 Example: A Game of Chance and Introducing enum (II) Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};  Starts at 1, increments by 1

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