A First Book of ANSI C Fourth Edition. Chapter 12 Structures

Similar documents
A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

INTRODUCTION 1 AND REVIEW

M.EC201 Programming language

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Introduction to C++ with content from

CSC 215 Structures. Dr. Achraf El Allali

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Data types, variables, constants

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Tokens, Expressions and Control Structures

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Multiple-Subscripted Arrays

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Subject: Fundamental of Computer Programming 2068

Computer Science Foundation Exam

A Fast Review of C Essentials Part I

Tin học cơ sở 4$ Structures!

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

CS240: Programming in C

BLM2031 Structured Programming. Zeyneb KURT

EECS 388 C Introduction. Gary J. Minden August 29, 2016

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Programming in C. 3. Pointers and Structures. Dr. Neel Krishnaswami University of Cambridge

A First Book of ANSI C

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

CS 0449 Sample Midterm

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

3.3 Structures. Department of CSE

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

(2-1) Numeric Expressions in C H&K Chapter 2. Instructor - Andrew S. O Fallon CptS 121 (August 27, 2018) Washington State University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Chapter 13 Control Structures

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Chapter 7 : Arrays (pp )

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CHAPTER 7 OBJECTS AND CLASSES

Goals of this Lecture

Fundamental of Programming (C)

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Basic Types, Variables, Literals, Constants

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

typedef int Array[10]; String name; Array ages;

Technical Questions. Q 1) What are the key features in C programming language?

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Computational Expression

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

M1-R4: Programing and Problem Solving using C (JULY 2018)

Use of scanf. scanf("%d", &number);

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

Lecture 3: C Programm

C Structures & Dynamic Memory Management

EMBEDDED SYSTEMS PROGRAMMING More About Languages

Chapter 1 & 2 Introduction to C Language

Contents. Custom data type struct data type Fixed length memory Alias data type

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

CS 161 Exam II Winter 2018 FORM 1

Name Section: M/W or T/TH. True or False (14 Points)

UEE1302 (1102) F10: Introduction to Computers and Programming

C Programming Review CSC 4320/6320

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Pointer Declarations

Systems Programming. 05. Structures & Trees. Alexander Holupirek

CHAPTER 7 OBJECTS AND CLASSES

6.096 Introduction to C++ January (IAP) 2009

QUIZ. What is wrong with this code that uses default arguments?

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

Writing Functions in C

3. Java - Language Constructs I

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

F4104 ALGORITHM & DATA STRUCTURE

Conditionals and Loops

Topics. Chapter 5. Equality Operators

Computer Programming & Problem Solving ( CPPS )

TILE PROCESSOR APPLICATION BINARY INTERFACE

Transcription:

A First Book of ANSI C Fourth Edition Chapter 12 Structures

Objectives Single Structures Arrays of Structures Passing and Returning Structures Unions (Optional) Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition 2

Introduction Each data item listed in Figure 12.1 is an entity by itself, called a data field Together, all the data fields form a single unit called a record In C, a record is referred to as a structure A First Book of ANSI C, Fourth Edition 3

Introduction (continued) A First Book of ANSI C, Fourth Edition 4

Introduction (continued) A structure s form consists of the symbolic names, data types, and arrangement of individual data fields in the record The structure s contents consist of the actual data stored in the symbolic names A First Book of ANSI C, Fourth Edition 5

Introduction (continued) A First Book of ANSI C, Fourth Edition 6

Structure definition in C: struct { int month; int day; int year; Single Structures } birth; Reserves storage for the individual data items listed in the structure The three data items are the members of the structure Assigning actual data values to the data items of a structure is called populating the structure A First Book of ANSI C, Fourth Edition 7

Single Structures (continued) spacing of a structure definition is not rigid A First Book of ANSI C, Fourth Edition 8

Single Structures (continued) Multiple variables can be defined in one statement struct {int month; int day; int year;} birth, current; Common to list the form of the structure with no following variable names The list of structure members must be preceded by a user-selected structure type name struct Date { int month; int day; int year; }; A First Book of ANSI C, Fourth Edition 9

Single Structures (continued) By convention the first letter of user-selected structure type names is uppercase A First Book of ANSI C, Fourth Edition 10

Single Structures (continued) Initialization of structures follows the same rules as for the initialization of arrays: struct Date birth = {12, 28, 1987}; Structure members can be of any data type struct PayRecord { char name[20]; int idnum; double regrate; double otrate; }; struct PayRecord employee = {"H. Price", 12387, 15.89, 25.50}; A First Book of ANSI C, Fourth Edition 11

Single Structures (continued) Advantage of structures is when the same structure type is used in a list many times Individual members can be arrays and structures struct { char name[20]; struct Date birth; } person; Example: person.name[4] A First Book of ANSI C, Fourth Edition 12

Arrays of Structures A First Book of ANSI C, Fourth Edition 13

Arrays of Structures (continued) A First Book of ANSI C, Fourth Edition 14

Arrays of Structures (continued) Inner braces are not necessary A First Book of ANSI C, Fourth Edition 15

Arrays of Structures (continued) Without explicit initializers, the numeric elements of both static and external arrays or structures are initialized to 0 (or nulls) An inferior alternative to an array of structures is parallel arrays Parallel arrays are two or more arrays, where each array has the same number of elements and the elements in each array are directly related by their position in the arrays They are rarely used any more A First Book of ANSI C, Fourth Edition 16

Passing and Returning Structures Individual structure members may be passed to a function in the same manner as any scalar variable display(emp.idnum) calcpay(emp.payrate,emp.hours); On most compilers, complete copies of all members of a structure can also be passed to a function by including the name of the structure as an argument to the called function calcnet(emp); A First Book of ANSI C, Fourth Edition 17

Passing and Returning Structures (continued) Although the structures in main() and calcnet() use the same globally defined structure type, this is not strictly necessary (although it is preferable) Pass by value A First Book of ANSI C, Fourth Edition 18

Passing and Returning Structures (continued) A structure can be passed by reference calcnet(&emp); double calcnet(struct Employee *pt) (*pt).idnum or *pt->idnum A First Book of ANSI C, Fourth Edition 19

Passing and Returning Structures (continued) A First Book of ANSI C, Fourth Edition 20

Passing and Returning Structures (continued) A First Book of ANSI C, Fourth Edition 21

Passing and Returning Structures (continued) ++ and -- can be applied to structures ++pt->hours (pt++)->hours (++pt)->hours A First Book of ANSI C, Fourth Edition 22

Passing and Returning Structures (continued) A First Book of ANSI C, Fourth Edition 23

Returning Structures A First Book of ANSI C, Fourth Edition 24

Returning Structures (continued) A First Book of ANSI C, Fourth Edition 25

Unions A union is a data type that reserves the same area in memory for two or more variables union { char key; int num; double price; } val; Each of these types, but only one at a time, can actually be assigned to the union variable A union reserves sufficient memory locations to accommodate its largest member s data type A First Book of ANSI C, Fourth Edition 26

Unions (continued) Individual union members are accessed using the same notation as structure members Typically, a second variable keeps track of the current data type stored in the union switch(utype) { case c : printf("%c", val.key); break; case i : printf("%d", val.num); break; case d : printf("%f", val.price); break; default : printf("invalid type : %c", utype); } A First Book of ANSI C, Fourth Edition 27

Unions (continued) A type name can be associated with a union to create templates union DateTime { long days; double time; }; union DateTime first, second, *pt; Pointers to unions use the same notation as pointers to structures Unions may be members of structures and arrays; structures, arrays, and pointers may be members of unions A First Book of ANSI C, Fourth Edition 28

Unions (continued) struct { char utype; union { char *text; double rate; } utax; } flag; rate is referenced as flag.utax.rate The first character of the string whose address is stored in the pointer text is accessed as *flag.utax.text A First Book of ANSI C, Fourth Edition 29

Common Programming Errors Attempting to use structures and unions, as complete entities, in relational expressions Assigning an incorrect address to a pointer that is a member of a structure or union Storing one data type in a union and accessing it by the wrong variable name can result in an error that is particularly troublesome to locate A First Book of ANSI C, Fourth Edition 30

Common Compiler Errors A First Book of ANSI C, Fourth Edition 31

Summary A structure allows individual variables to be grouped under a common variable name A structure type name can be used to create a generalized structure type describing the form and arrangement of elements in a structure Structures are particularly useful as elements of arrays A First Book of ANSI C, Fourth Edition 32

Summary (continued) Individual members of a structure are passed to a function in the manner appropriate to the data type of the member being passed Structure members can be any valid C data type, including structures, unions, arrays, and pointers Unions are declared in the same manner as structures A First Book of ANSI C, Fourth Edition 33