StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
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.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
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.
C Programming Lecture 23 Enumeration Types Structures.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
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.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
Week 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Data Structures Using C++ 2E
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
 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.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
© 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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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.
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.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 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.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
File Processing (Cont.) and Structures
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Structures Declarations CSCI 230
Presentation transcript:

StructuresStructures Systems Programming

StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions Using Structures with Functions Structure Example Structure Example Systems Programming Structures 2

10.1 Introduction Structures – –A collection of related variables (aggregated) under one name. Can contain variables of different data types. – –Commonly used to define records to be stored in files. * When combined with pointers, structures can create linked lists, stacks, queues, and trees Pearson Ed -All rights reserved. Systems Programming Structures 3

Structures Example 1: struct player { char *name; int num; char *team; char *pos; } ; /* Dont forget the semicolon! */ structure tag structure members Systems Programming Structures 4

Structures Example 1: struct player { char *name; int num; char *team; char *pos; } player1, player2; structure tag structure members Declare two players Systems Programming Structures 5

Typedef Example Example 2: struct card { const char *face; const char *suit; } ; typedef struct card Card; 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 Pearson Ed -All rights reserved. The new type Card is an alias for type struct card. Systems Programming Structures 6

typedef Another way to declare this!! typedef struct { const char *face; const char *suit; } Card; … Card deck[52]; 2007 Pearson Ed -All rights reserved. Systems Programming Structures 7

10.6 typedef Example: typedef struct Card *CardPtr; or Card *Cardptr; – –Defines a new type name CardPtr as an alias for type struct Card *. – –typedef does not create a new data type. It only creates an alias. Capitalize the first letter of typedef names to emphasize that they are synonyms for other type names Pearson Ed -All rights reserved. Systems Programming Structures 8

10.2 Structure Definitions struct information – –A struct cannot contain an instance of itself. – –It can contain a member that is a pointer to the same structure type (a self-referential structure). – –A structure definition does not reserve space in memory. Rather a struct 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 Ed -All rights reserved. Systems Programming Structures 9

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 Pearson Ed -All rights reserved. Systems Programming Structures 10

10.3 Initializing Structures Initializer lists – –Example: struct card oneCard = { "Three", "Hearts" }; Assignment statements – –Example: struct card threeHearts = oneCard; – –Could also define and initialize threeHearts as follows: struct card threeHearts; threeHearts.face = Three; threeHearts.suit = Hearts; 2007 Pearson Ed -All rights reserved. Systems Programming Structures 11

10.4 Accessing Members of Structures Accessing structure members –{the structure member operator} –The dot operator (. ) {the structure member operator} is used to access a structure member via the structure variable name. card myCard; printf( "%s", myCard.suit ); –{the structure pointer operator} –The arrow operator ( -> ) {the structure pointer operator} accesses a structure member via a pointer to the structure. card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); – –myCardPtr->suit is equivalent to ( *myCardPtr ).suit 2007 Pearson Ed -All rights reserved. Systems Programming Structures 12

Structure member and pointer operators Structure definition Structure definition must end with semicolon Dot operator accesses members of a structure 2007 Pearson Ed -All rights reserved. Systems Programming Structures 13

Arrow operator accesses members of a structure pointer 2007 Pearson Ed -All rights reserved. Structure member and pointer operators Systems Programming Structures 14

10.5 Using Structures with Functions Passing structures to functions – –The entire structure can be passed. – –Individual members of the structure can be passed. – –For both cases, they are passed by value. To pass a structure by-reference – –Pass the address of the structure variable. To pass arrays by-value – –Create a structure with the array as a member and then pass the structure Pearson Ed -All rights reserved. Systems Programming Structures 15

A Structure Example Each card has a face and a suit Card is now an alias for struct card 2007 Pearson Ed -All rights reserved. Systems Programming Structures 16

Constant pointer to modifiable array of Card s Fills the deck by giving each Card a face and suit 2007 Pearson Ed -All rights reserved. A Structure Example Systems Programming Structures 17

Each card is swapped with another, random card, shuffling the deck 2007 Pearson Ed -All rights reserved. A Structure Example Systems Programming Structures 18 ? is part of conditional operator (only ternary operator in C) see page 76!!

A Structure Example Systems Programming Structures 19

Review of Structure Definition of structures in C Syntax details for declaring structs Initializing structs Typedef Structure member (.) and pointer -> operators Passing structures to functions A Structure Example Systems Programming Structures 20