StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.

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.
Programming in C Chapter 10 Structures and Unions
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.
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
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.
© 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.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Cartomancy. Cartomancy (using cards to tell fortune) - cartomancy, there are mainly 24-card decks (from the ace up to nines), 32-card decks (from the.
CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Two Way Tables Venn Diagrams Probability. Learning Targets 1. I can use a Venn diagram to model a chance process involving two events. 2. I can use the.
Poker. Basic card terminology Suits: Hearts, diamonds, clubs, spades Deuce, face cards.
15.3 Counting Methods: Combinations ©2002 by R. Villar All Rights Reserved.
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,
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
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.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
CLICK THE NUMBERS IN SEQUENCE
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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
11.9: Solving Probability Problems by Using Combinations
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
Introduction to Programming
C Structures, Unions, Bit Manipulations and Enumerations
CLICK THE NUMBERS IN SEQUENCE
CLICK THE NUMBERS IN SEQUENCE
Structures Declarations CSCI 230
Presentation transcript:

StructureStructure

Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions typedef Example: High-Performance Card Shuffling and Dealing Simulation

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

Structure definition 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

Structure Definition 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;

Structure Definition

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

Initializing structure 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”;

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

Ace of Spades

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

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

THANK YOU…