16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.

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

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.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Programming Lecture 23 Enumeration Types Structures.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
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.
Structures Spring 2013Programming and Data Structure1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
 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.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Data Types.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6 Structures,
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures 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.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
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 Curt Hill The C/C++ switch Statement A multi-path decision statement.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
 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.
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.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Structures, Unions, Enumerations
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers, Enum, and Structures
Structure, Unions, typedef and enumeration
Chapter 7 - Pointers Outline 7.1 Introduction
TMF1414 Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Derived types.
Standard Version of Starting Out with C++, 4th Edition
Structures, Unions, and Enumerations
Presentation transcript:

16. STRUCTURES, UNIONS, AND ENUMERATIONS

Declaring Structures A structure is a collection of one or more components (members), which may be of different types. (Structures are called records in many other programming languages; members are known as fields.) Declaring structure variables can be done as follows: struct { char name[25]; int id, age; char sex; } s1, s2;

Declaring Structures Member names don’t conflict with any other names in a program. Structure variables may be initialized: struct { char name[25]; int id, age; char sex; } s1 = { "Smith, John", 2813, 25, 'M'}, s2 = { "Smith, Mary", 4692, 23, 'F'};

Structure Tags A structure can be given a name (a structure tag) for later use: struct student { char name[25]; int id, age; char sex; }; struct student s1, s2; The two declarations can be combined if desired, with the same effect: struct student { char name[25]; int id, age; char sex; } s1, s2;

Structure Tags As an alternative, we can use typedef to name a structure: typedef struct { char name[25]; int id, age; char sex; } Student; Student s1, s2;

Accessing the Members of a Structure The members of a structure are accessed by writing first the name of the structure, then a period, then the name of the member: struct student { char name[25]; int id, age; char sex; } s; strcpy(s.name, "Doe, John"); s.id = 18193; s.age = 18; s.sex = 'M';

Accessing the Members of a Structure Arrays and structures can be nested within one another: struct student class[30]; strcpy(class[12].name, "Doe, John"); class[12].id = 18193; class[12].age = 18; class[12].sex = 'M';

Operations on Whole Structures C allows three operations on whole structures. Assignment of whole structures: s1 = s2; /* s1 and s2 are structures of the same type */ Passing a whole structure to a function: f(s1); Returning a whole structure from a function: s1 = f(s2); No other operations are allowed, including testing whole structures for equality.

Operations on Whole Structures All three operations involve copying an entire structure, which can be time consuming if the structure is large.

Unions A union is similar to a structure, except that its members are overlaid (located at the same memory address). Example: union { int i; double d; } u; The members of a union are accessed in the same way as members of a structure: u.i = 15; or u.d = 8.89; Since u.i and u.d have the same memory address, changing the value of one alters the value of the other.

Unions Unions have the same properties as structures: Unions can be initialized (the initializer specifies the value of the first member). Unions can be identified by tags or type names. Unions can be assigned, passed to functions, and returned by functions. Uses of unions: To save space. To view a data item in two or more different ways. To build heterogeneous data structures.

Enumerations An enumeration is a collection of named integer constants. Enumerations are defined in a manner similar to structures and unions: enum bool {FALSE, TRUE}; enum rank {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}; enum suit {CLUBS, DIAMONDS, HEARTS, SPADES}; enum EGA_colors {BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LT_GRAY, DK_GRAY, LT_BLUE, LT_GREEN, LT_CYAN, LT_RED, LT_MAGENTA, YELLOW, WHITE};

Adding a “Tag Field” to a Union There is no way to tell which member of a union was last assigned. To retain this information, we can create a structure with two members: (1) the union itself, and (2) a “tag field” whose value indicates which member of the union was last assigned. struct { enum {INT_KIND, DOUBLE_KIND} kind; /* tag field */ union { int i; double d; } u; } s; The tag field is often an enumeration.

Adding a “Tag Field” to a Union When we assign to a member of the union, we also set the kind member: s.kind = INT_KIND; s.u.i = 15; We can test the kind member before accessing the union: if (s.kind == INT_KIND) printf("%d", s.u.i); else if (s.kind == DOUBLE_KIND) printf("%g", s.u.d);

New Features in C99 C99 has two new features that are related to structures and unions: Designated initializers Compound literals These features can also be used with arrays.

New Features in C99 Here is the usual way to initialize a structure: struct { int i; double d; } s = {1, 2.5}; Within a designated initializer, the values of the members can be listed in any order, provided that each value is accompanied by a member name: struct { int i; double d; } s = {.d=2.5,.i=1}; Any members that are not listed in the initializer will be given a default value.

New Features in C99 In the case of a union, only one member can be initialized: union { int i; double d; } u = {.d=2.5}; A designated initializer for an array looks like this: int a[10] = {[5]=6, [8]=2, [1]=3}; which is equivalent to writing int a[10] = {0, 3, 0, 0, 0, 6, 0, 0, 2, 0};

New Features in C99 A designated initializer may also contain values in ordinary positional notation. Using compound literals, it becomes possible to create and initialize structures, unions, and arrays “on the fly,” without storing them in variables. A compound literal consists of a type name in parentheses (similar to a cast expression), followed by a set of values in braces (similar to an initializer).

New Features in C99 In the following example, a point structure is being created and passed to the move_to function: move_to((struct point){5, 10}); In older versions of C, it would be necessary to create a name for the structure before passing it to the function: struct point pos = {5, 10}; move_to(pos); The following example creates a three-element array and sets p to point to the first element in the array: int *p = (int []){1, 2, 3}; By default, a compound literal is an lvalue, so its values can be changed.