1 240-222 CPT: Ptr+Str/11 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.

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.
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.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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.
New features in JDK 1.5 Can these new and complex features simplify Java development?
Programming in C Chapter 10 Structures and Unions
© 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.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
EXAMPLES. Example 1: Write a Java method that performs addition on two binary numbers. Each binary number is kept in an integer array
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
© 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.
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
Refreshing Your Skills for Chapter 10.  If you flip a coin, the probability that it lands with heads up is 1/2.  If you roll a standard die, the probability.
Introduction to Data Structures Systems Programming.
 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.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
© 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,
Section 11.4 Tree Diagrams, Tables, and Sample Spaces Math in Our World.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Introduction to Data Structures Systems Programming Concepts.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 3 – Pointers.
 2000 Deitel & Associates, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf 1 5.1Introduction Pointers –Powerful, but difficult to master.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
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.
1 CSC 211 Data Structures Lecture 5 Dr. Iftikhar Azim Niaz 1.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Chapter 7 - Pointers Outline 7.1 Introduction
Structure, Unions, typedef and enumeration
Chapter 7 - Pointers Outline 7.1 Introduction
File Processing (Cont.) and Structures
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to Programming
Presentation transcript:

CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs 11. Pointers and Structures

CPT: Ptr+Str/11 Overview 1.A struct can contain Strings 2.Shuffling Cards 3.Pointers to structs 4.Updating a struct

CPT: Ptr+Str/11 1. A struct can Contain Strings struct card{ char *face; char *suit; }; : struct card a = {"Three", "Hearts"}; : printf("%s", a.suit); /* prints Hearts */

CPT: Ptr+Str/11 2. Shuffling CardsSec Shuffling CardsSec #include #include #include struct card { char *face; char *suit; }; typedef struct card Card; void filldeck(Card [], char *[], char *[]); void shuffle(Card []); void deal(Card []); continued

CPT: Ptr+Str/11 int main() { Card deck[52]; char *face[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten","Jack", "Queen", "King"}; char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"}; srand(clock());/* set random num */ filldeck(deck, face, suit); shuffle(deck); deal(deck); return 0; } continued

CPT: Ptr+Str/11 void filldeck(Card wdeck[], char *wface[], char *wsuit[]) /* initialise the deck of cards */ { int i; for (i = 0; i <= 51; i++) { wdeck[i].face = wface[i % 13]; wdeck[i].suit = wsuit[i / 13]; } } continued

CPT: Ptr+Str/11 void shuffle(Card wdeck[]) /* randomly rearrange the deck of cards */ { int i, j; Card temp; for (i = 0; i <= 51; i++) { j = rand() % 52; /* get rand num */ if (i != j) { temp = wdeck[i]; wdeck[i] = wdeck[j]; wdeck[j] = temp; } } } continued

CPT: Ptr+Str/11 void deal(Card wdeck[]) /* display the deck */ { int i; for (i = 0; i <= 51; i++) printf("%5s of %-8s%c", wdeck[i].face, wdeck[i].suit, (i + 1) % 2 ? '\t' : '\n'); }

CPT: Ptr+Str/11 Output (see fig. 10.4): Eight of DiamondsAce of Hearts Eight of ClubsFive of Spades : :

CPT: Ptr+Str/11 3. Pointers to Structs struct card a, *c; a = {"Four", "Spades"}; c = &a; : printf("%s", c->suit); /* prints Spades */ c->suit is equivalent to (*c).suit

CPT: Ptr+Str/11 #include /* fig 10.2 */ struct card { char *face; char *suit; }; continued Example

CPT: Ptr+Str/11 int main() { struct card a; struct card *aptr; a.face = "Ace"; a.suit = "Spades"; aptr = &a; printf("%s of %s\n%s of %s\n%s of%s\n", a.face, a.suit, aptr->face, aptr->suit, (*aptr).face, (*aptr).suit); return 0; }

CPT: Ptr+Str/11 Output: Ace of Spades Ace of Spades Ace of Spades

CPT: Ptr+Str/11 4. Updating a struct l Code fragment: struct card { char *face; char *suit; }; int main() { struct card a; a.face = “Three; printf(“%s”, a.face); }

CPT: Ptr+Str/11 Discussion l This code works, so what is the problem? Answer: the scope of “Three” –at the end of main(), “Three” goes out of scope and is deleted –this does not matter for this example because the scope of main() is the entire program

CPT: Ptr+Str/11 A More Dangerous Example int main() { struct card a; a = initialise(); printf(“%s”, a.face); : printf(“%s”, a.face); } struct card initialise(void) { struct card b; b.face = “Three”; return b; }

CPT: Ptr+Str/11 Discussion The scope of “Three” is initialise(), and so will be deleted after initialise() returns –return copies out the b struct, including its two pointers –a is assigned the pointers, but what do they point at? –the deletion may not happen immediately, but depend on the memory usage of the rest of the program the first printf() may work, sometimes!

CPT: Ptr+Str/11 First Solution struct card1( char face[10]; char suit[10]; }; int main() { struct card1 a; a = initialise1(); : printf(“%s”, a.face); } struct card1 initialise1(void) { struct card1 b; strcpy(b.face, “Three”); return b; }

CPT: Ptr+Str/11 Discussion l The general solution is to make a copy of the string –“Three” is copied into the fixed size array b.face using strcpy() –that array is copied out as part of the b struct using return

CPT: Ptr+Str/11 Second Solution struct card( char *face; /* no fixed size */ char *suit; }; int main() { struct card a; a = initialise2(); : printf(“%s”, a.face); } struct card initialise2(void) { struct card b; b.face = (char *)malloc(6); strcpy(b.face, “Three”); return b; }

CPT: Ptr+Str/11 Discussion The second solution still uses copying, but now calls malloc() to make dynamic memory –return only copies out the pointers inside b –but malloc’ed memory is not deleted even though the scope of b is initialise2() –so a.face will point to the malloc’ed memory from initialise2()

CPT: Ptr+Str/11 Third Solution struct card( char *face; /* no fixed size */ char *suit; }; int main() { struct card a; initialise3(&a); : printf(“%s”, a.face); } void initialise3(struct card *ap) { ap->face = (char *)malloc(6); strcpy(ap->face, “Three”); }

CPT: Ptr+Str/11 Discussion The third solution uses malloc() to make dynamic memory, but for the a struct. –pass in a pointer to a, and alter a via the pointer (this is how C implements call-by-reference). –this is the most common coding style for manipulating structs inside functions