Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.

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.
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.
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.
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”)
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.
Line Efficiency     Percentage Month Today’s Date
 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,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Dale Roberts, Lecturer Computer Science, IUPUI.
Enum, Struct, & Union typedef - a type definition, typedef, gives a name to a data type by creating a new type that can then be used anywhere a type is.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
21 August (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.
Programming in C Structs and Unions. Java vs C Suppose you were assigned a write an application about points and straight lines in a coordinate plane.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
© 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.
 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.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
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.
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.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
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 *
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Jan 2016 Solar Lunar Data.
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Average Monthly Temperature and Rainfall
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Derived types.
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Chapter: 7-12 Final exam review.
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations

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.

char name[50][32]; long id[50]; int chi[50]; int eng[50]; float avg[50];

struct Stu_Type { char name[32]; long id; int chi; int eng; float avg; }; int i; struct Stu_Type x;

strcpy(x.name,Chin-Shiuh Shieh); x.id=70523; x.chi=50; x.eng=60; x.avg=(x.chi+x.eng)/2.0;

struct Stu_Type stu[50]; for(i=0;i<50;i++) { /* fscanf(fpr,%s %ld %d %d,name[i],&id[i],&chi[i],&eng[i]); avg[i]=(chi[i]+eng[i])/2.0; */ fscanf(fpr,%s %ld %d %d,stu[i].name,&stu[i].id,&stu[i].chi,&stu[i].eng); stu[i].avg=(stu[i].chi+stu[i].eng)/2.0; }

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.1 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

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 * These members are face and suit

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

10.2 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

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

10.4 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

10.5 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.6 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

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

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

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

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

10.11 Enumeration Constants Fig10_18.c