Structures, Unions, and Enumerations

Slides:



Advertisements
Similar presentations
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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”)
Structures Spring 2013Programming and Data Structure1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
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.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6 Structures,
Chapter 16: Structures, Unions, and Enumerations Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 16 Structures, Unions, and Enumerations.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Array, Structure and Union
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
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.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
+ 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.
Structure Types Suppose that a program needs to declare several structure variables with identical members. We need a name that represents a type of structure,
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
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.
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.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
C Structures and Memory Allocation
LESSON 06.
Chapter 8 Arrays, Strings and Pointers
Exam 3 Coverage (everything up to strings) Two Sections Pointers
Structures, Unions, Enumerations
EGR 2261 Unit 10 Two-dimensional Arrays
11 Chapter Structured Data
Chapter 12 Enumerated, Structure, and Union Types Objectives
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
Structures, Unions, and Enumerations
TMF1414 Introduction to Programming
CS150 Introduction to Computer Science 1
Module 2 Arrays and strings – example programs.
JavaScript: Functions.
Arrays in C.
DATA HANDLING.
Structures Lesson xx In this module, we’ll introduce you to structures.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Programming in C #4 Structs and Unions.
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 3 Introduction to Classes, Objects Methods and Strings
7 Arrays.
הגדרת משתנים יום שלישי 27 נובמבר 2018
Derived types.
Structures, Unions, and Enumerations
Structures, Unions, and Enumerations
Chapter 11: Structured Data.
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Structures, Unions, and Enumerations
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Structs Adapted from Dr. Mary Eberlein, UT Austin.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
Standard Version of Starting Out with C++, 4th Edition
C Language B. DHIVYA 17PCA140 II MCA.
Introduction to Pointers
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Structures, Unions, and Enumerations Chapter 16 Structures, Unions, and Enumerations A structure is a collection of values (members), possibly with different types. A union is similar to a structure except its members share the same storage A union can store one member at a time, but not all members simultaneously. An enumeration is an integer type, values are named by the programmer

struct { int number ; char name[NAME_LEN+l] ; int on_hand; } part1, part2; The semicolon must be present to terminate the declaration. Each structure variable has three members: number (the part number), name (the name of the part) on_hand (the quantity on hand) struct { ... } specifies a type part1 and part2 are variables of that type.

Members of a structure are stored in memory in the order they are declared (1) part1 is stored at address 2000 (2) integers occupy two bytes (3) NAME_LEN has the value 25 (4) there are no gaps between the members part1 has the following appearance in memory:

Each structure represents a new scope Any names declared in the scope won’t conflict with other names in a program Each structure has a separate name space for its members The following declarations can appear in the same program: struct { int number; char name[NAME_LEN+1] ; char sex; } employee1, employee2; struct { int number; char name[NAME_LEN+1]; int on_hand ; } part1, part2; The number and name members in the part1 and part2 structures do not conflict with the number and name members in employee1 and employee2.

part1 after initialization Initializing Structure Variables Like an array, a structure variable may be initialized at the time it is declared. To initialize a structure, a list of values to be stored in the structure are enclosed in braces The values in the initializer must appear in the same order as the members of the structure. struct { int number ; char name[NAME_LEN+l]; int on_hand; } part1 = {528, "Disk drive", 10}, part2 = {914, "Printer cable", 5}; part1 after initialization :

In other languages; structures are called records; members of structures are known as fields. A structure has properties quite different from an array's: The elements of a structure may have different types. Each member of a structure has a name To select a particular member, we specify its name, not its position.

To access a member within a structure; The name of the structure first, then a period, then the name of the member. For example, the following will display the values of part1 members: printf("Part number: %d\n", part1.number); printf("Part name: %s\n", part1.name); printf("Quantity on hand: %d\n", part1.on_hand); scanf(“%d”, &part1.on_hand); &part1 .on_hand contains two operators (& and .). The . operator takes precedence over the & operator & computes the address of part1. on_hand

The structure operation assignment (=): Arrays can not be copied using = but structures can. part2 = part1; part2.number will contain the same value as part1.number part2.name will be the same as part1.name part2.on_hand will be the same as part1.on_hand

C provides two ways to name structures: (1) structure tag – a name used to identify a particular kind of structure. (2) typedef - used to define a type name. The name of the type, Part, comes at the end of the definition, do not write struct Part. structure tag named part typedef variables named Part struct part { typedef struct { int number; int number; char name[NAME_LEN+1]; char name[NAME_LEN+l] ; int on_hand; int on_hand; }; } Part; Part can be used the same way as built-in types: Part part1, part2; All Part variables, are compatible. The declaration of a structure tag can be combined with declaration of structure variables such as part1 and part2 that are type Part.

struct part { int number; char name[NAME_LEN+1] ; int on_hand; } part1, part2 ; All structures declared to have type struct part are compatible: struct part part1 = {528, "Disk drive", 10}; struct part part2; part2 = part1; /* legal; both parts have the same type */

Passing a structure to a function and returning a structure from a function both require a lot of overhead, Therefore, it is advisable to pass a pointer to a structure instead of passing the structure itself. In the following example, the initializer for part2 is the parameter passed to the f function: void f(struct part part1) { struct part part2 = part1; … } The initializer can be any expression of the proper type, including a function call that returns a structure.

Nested Structures struct person_name { char first[FIRST_NAME_LEN+1]; char middle_initial; char last[LAST_NAME_LEN+1] ; }; We can use the person_name structure as part of a larger structure: struct student { struct person_name name; int id, age; char sex; } student1, student2; Accessing student1's first name, middle initial, or last name requires two applications of the . operator: strcpy(student1.name.first, "Fred")

Arrays of Structures The following array is capable of storing information about 100 parts: struct part inventory[100]; To print the part stored in position i, for example: print_part(inventory[i]) ; Accessing a member within a part structure requires a combination of sub- scripting and member selection. To assign 883 to the number member of inventory [ i ]: inventory[i].number = 883;

Accessing a single character in a part name requires subscripting (to select a particular part), followed by selection (to select the name member), followed by subscripting (to select a character within the part name). To change the name stored in inventory [ i ] to an empty string, we could write inventory[i].name[0] = '\0';

Initializing an array of structures Initializing is done in much the same way as initializing a multidimensional array Each structure has its own brace-enclosed initializer; The initializer for the array simply wraps another set of braces around the structure initializers.