Structures, Unions, and Enumerations Chapter 16. 2 Data Structure ( 資料結構 ) Content of data –Attributes of an object Person:name, age, sex, … A poker card:suit.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
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
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures in C.
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.
Dale/Weems/Headington
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Structures EE2372 Software Design I Dr. Gerardo Rosiles.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Functions, Pointers, Structures Keerthi Nelaturu.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
© 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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
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 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
CS1010 Programming Methodology
Chapter 10-1: Structure.
Pointers, Enum, and Structures
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Pointers and References
C Structures, Unions, and Enumerations
Pointers, Dynamic Data, and Reference Types
Structures, Unions, and Enumerations
Presentation transcript:

Structures, Unions, and Enumerations Chapter 16

2 Data Structure ( 資料結構 ) Content of data –Attributes of an object Person:name, age, sex, … A poker card:suit ( 花色 ), face ( 點數 ) –Columns of a record Book-Borrowing:book_title, book_ID, borrower, date It is convenient if we can save them in a structure. –data structure ( 資料結構 )

3 Structure Types (16.2) Syntax: struct structureName{ dataType member1; dataType member2;... };  Ex: struct personData{ char name[20]; int age; bool sex; }; A pair of curve parentheses NOTE!! Ended with ;

4 Structure Variables (16.1) struct defines a new variable type ( 資料 型態 ), not a new variable. We can use a defined structure type to define variables. Ex:  personData player1, player2;  personData student[10];

5 Declaring Structure Variables  struct personData{ char name[20]; int age; bool sex; };  personData player1, player2; player1player2

6 Declaring Structure Variables  struct personData{ char name[20]; int age; bool sex; };  personData player1 = {" 莊孝維 ", 19, false}, player2 = {" 林志零 ", 20, true}; player1player2 agesex name agesex name members; fields records

7 Declaring Structure Array (16.3)  personData student[10]; student[0]student[9] ‧‧‧ student[] student[1]

8 Initializing When Defining (16.3) Ex: personData student[10]={ {" 莊孝維 ", 19, false}, // student[0] {" 林志零 ", 20, true}, // student[1]... //name,age,sex }; The initial data are given in the order of members in the structure and the order of elements in the array.

9 Declaring Structure Variables The following statements are also legal:  struct personData a1,student[10];  struct info{ int a,b; } info1, info2;  struct { int a,b; } info1, info2; define variables immediately after the definition of a structure

10 Accessing Members of Structure '.' : structure member operator (dot operator) player1 player1.name player1.sex player1.age  See ->

11 Accessing Members of Structure '.' : structure member operator (dot operator) –Ex: personData player1; strcpy( player1.name, " 莊孝維 "); player1.age = 19; player1.sex = false; // true for female, false for male player1 agesex name

12 Practice Define a data structure to store data of a student. –What attributes do we need to save? What are their types? Define 100 users. Input some data (not necessary 100) from the keyboard. Print these data out.

13 Pointer to A Structure Ex: personData player1, *mPtr; mPtr = &player1; player1 mPtr

14 Accessing Members of Structure '->' : structure pointer operator (arrow operator) player1 mPtr->name mPtr->sex mPtr->age mPtr  See.

15 Accessing Members of Structure '->' : structure pointer operator (arrow operator) – 例: personData player1, *mPtr=&player1; strcpy(mPtr->name, " 莊孝維 "); mPtr->age = 19; mPtr->sex = false; mPtr player1 agesex name

16 Dot operator has higher preference than dereferencing operator. Accessing Members of Structure personData player1, *mPtr; mPtr = &player1; scanf("%d", &player1.age); scanf("%d", &(player1.age)); printf("%d\n", player1.age); printf("%d\n", mPtr.age); printf("%d\n", mPtr->age); printf("%d\n", (*mPtr).age); printf("%d\n", *mPtr.age);  

17 Pointer to A Structure NOTE!! A pointer is just a variable with 4 bytes. No memory of its reference type will be allocated at definition time. –Hence you have to define a real variable of this type and let pointer refer to it before you use this pointer. Ex: personData *mPtr; personData player1; mPtr = &player1; player1 mPtr

18 Structure Assignment You can assign a variable's content as the other variable's content directly by = if they are of the same structure. struct data{ int age,sex; };  data player1, player2;  player1.age = 34; player1.sex = 1;  player2 = player1; player1player2 age sex 341 1

19 Structure Comparison The same as strings, you cannot compare two variables of the same structure by ==.  For strings (i.e. char *), == means two pointers refer to the same address.  For structures, == is undefined. if (player1 == player2) … Compile error C2676: binary '==' : 'struct person' does not define this operator or a conversion to a type acceptable to the predefined operator

20 Structure Comparison So… –Write a function to compare two variables of the same structure by comparing all the members of the structure.

21 Passing Structure as Parameters myfunc(personData manager);  Note that it is call by value!  If you want to modify the content of the variable, please call by reference. myfunc(personData *manager); __ ↑ use structure name as variable type __ ↑ call by reference

22 Practice Write a function to compare if two personData variables have the same values. Write a function to initialize a personData variable as follows: {" 無名氏 ", 0, true} struct personData{ char name[20]; int age; bool sex; };

struct city { char name[20]; int population; double area; }; name areapopulation Taipei name areapopulation Keelung city Taipei = {"Taipei", }, Keelung = {"Keelung", }; Objects vs. Structures An Example

agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung person Mary = {"Mary", 32}, Susan = {"Susan", 5}; ? ? Mary agename birthplacelivingplace ?? Susan

agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung // Mary was born in Keelung and lives in Taipei now Mary.birthplace = &Keelung; Mary.livingplace = &Taipei; ? ? Mary agename birthplacelivingplace ?? Susan

agename birthplacelivingplace struct city { char name[20]; int population; double area; }; struct person { char name[20]; int age; city *birthplace, *livingplace; }; name areapopulation Taipei name areapopulation Keelung // Susan lives with Mary Susan.livingplace = Mary.livingplace; Mary agename birthplacelivingplace ?? Susan

27 Nested Structures (16.3) A member of a structure can be a structure of another type. Ex: struct dateData{ int year, month, day; }; struct personData{ char name[20]; dateData birthdate; };  personData mySon; mySon.birthdate.day = 26; mySon name birthdate yearmonthday 26

28 typedef (16.2) To give an alias name –typedef int ageType; ageType age[20]; To give a shorter name for "struct yourStructureName" –typedef struct personData pType; pType student[20]; original name new name original name new name

29 Examples of typedef  typedef int ageType;  typedef struct personData pType;  typedef personData * personPtr;  typedef struct{ char id[13],nickname[100]; int loginTimes,status; } bbsIDdata;