Chapter 7: Structuring the Data. Lecture7 1

Similar documents
12 CREATING NEW TYPES

Input And Output of C++

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Programming. Structures, enums and unions

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

BLM2031 Structured Programming. Zeyneb KURT

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

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

Chapter 10 C Structures, Unions, Bit Manipulations

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Lecture 3. The syntax for accessing a struct member is

Tokens, Expressions and Control Structures

Lecture10: Structures, Unions and Enumerations 11/26/2012

14. Other Data Types. Compound Data Types: Defined data types (typedef) Unions typedef existing_type new_type_name ;

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Basic Elements of C. Staff Incharge: S.Sasirekha

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Type Checking. Prof. James L. Frankel Harvard University

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

IV Unit Second Part STRUCTURES

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Pointers, Dynamic Data, and Reference Types

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Francesco Nidito. Programmazione Avanzata AA 2007/08

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Computer System and programming in C

Object-Oriented Principles and Practice / C++

A flow chart is a graphical or symbolic representation of a process.

Syntax and Variables

Chapter 6. Data Types

Lecture Notes on Programming Languages

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

CSE 230 Intermediate Programming in C and C++

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS Programming In C

Basic Types, Variables, Literals, Constants

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

ECE 122. Engineering Problem Solving with Java

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS201 - Introduction to Programming Glossary By

Structures and Union. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

Structures and Pointers

Computer Systems Principles. C Pointers

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

C-LANGUAGE CURRICULAM

Arrays III and Enumerated Types

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

CSC 1300 Exam 4 Comprehensive-ish and Structs

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Variables. Data Types.

A Fast Review of C Essentials Part I

DEPARTMENT OF MATHS, MJ COLLEGE

Review of the C Programming Language

More non-primitive types Lesson 06

EL2310 Scientific Programming

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Short Notes of CS201

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Arrays. Systems Programming Concepts

6.096 Introduction to C++ January (IAP) 2009

Review of the C Programming Language for Principles of Operating Systems

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

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

Data Type. structure. C Programming Lecture 11 : Structuret. struct union. typedef. A collection of one or more variables

Programming in Haskell Aug-Nov 2015

[0569] p 0318 garbage

Lecture 02 C FUNDAMENTALS

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Introduction to C++ Systems Programming

COMP 2355 Introduction to Systems Programming

UNIVERSITY OF MALTA FACULTY OF INFORMATION AND COMMUNICATION TECHNOLOGY Department of Computer Information Systems

Data Structures Unit 02

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

ECET 264 C Programming Language with Applications. Lecture 13. Structures and Unions

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 8: Intraprogram Communication. Lecture 8 1

Lecture 12: Data Types (and Some Leftover ML)

Programming for Engineers Structures, Unions

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Chapter 7. Additional Control Structures

Enumerated Types. Mr. Dave Clausen La Cañada High School

Organization of Programming Languages CS3200 / 5200N. Lecture 06

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Reserved Words and Identifiers

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Chapter 6. Data Types ISBN

Lecture 3: C Programm

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

COP4020 Programming Assignment 1 - Spring 2011

Transcription:

Chapter 7: Structuring the Data Lecture7 1

Topics Introduction to Structures Operations on Structures Using Structures with Arrays Using Structures with Pointers Passing Structures to and from Functions Bit Fields Enumerated Types Unions Lecture 7 2

Structures A structure is a data type that can hold multiple fields of multiple types of data. A structure is not an array, an array holds only a table of the same type. A structure is a user defined data type. Instead of float, int, or char, the user creates a type of data. The keyword for this construction is struct. Lecture 7 3

Struct struct tag { member_type member_name; member_type member_name; member_type member_name; }; Lecture 7 4

Structures Why structures? Better to describe the real world The world is more than just one set of numbers The world is more than just one kind of data Good to hold associated data Address books phone books calendars Now a method of keeping a record is available, new methods can be applied Lecture 7 5

Struct struct book { char title[1024]; char author[1024]; double dewy; long pages; int year; }; struct book book1; Lecture 7 6

Struct Each portion of a struct is called a field. To access the field of a struct, a period is used. By doing this, each field can be seen by the statements of the program. Lecture 7 7

Struct To create a variable using this structure, the struct can be used later on. Structure can be created without a name tag for the struct, but will not be able to be used for declaring variables. Lecture 7 8

Struct struct { char title[1024]; char author[1024; double dewy; long pages; int year; } book1, book2; Lecture 7 9

Struct struct book WarAndPeace; Will contain nonsense Fields must be filled separately Lecture 7 10

Struct struct book Almanac={ "2006 Almanac", "Richard Poorly", 1022.322, 365, 2006 }; Each data field is the right type Each data field is in the right order Will be correct Lecture 7 11

Struct struct book Encyclopedia={"Americana 2003"}; Will only have some fields correct. Uninitialized arrays and strings may contain nonsense Numeric fields will contain zeros Lecture 7 12

Struct Each field of a struct is called a member Each member can be accessed with a period. variable_name.member_name; Lecture 7 13

Struct void ShowBook(struct book thisbook) { printf("title:\t%s\n",thisbook.title); printf("author:\t%s\n",thisbook.author); printf("year:\t%04d\n",thisbook.year); printf("dewy:\t%f\n",thisbook.dewy); printf("pages:\t%d\n",thisbook.pages); printf("\n"); } Lecture 7 14

Struct struct book DayBook; strcpy(daybook.title,"history"); strcpy(daybook.author,"heroditous"); DayBook.year=15; DayBook.dewy=0.001; DayBook.pages=2200; Lecture 7 15

Operations on Structures The period. to access a member. The sizeof() command to find out the size. The address operator &, can be used to find the address of a struct. The assignment operator, = assigns the contents of one structure to another. Lecture 7 16

Using Structs with Arrays An array can be made out of any data type, including a user defined struct. struct book booklist[5]; Lecture 7 17

Using Structs with Arrays To reference a single cell, then the subscript or index is referenced. booklist[3].title booklist[3].author[5]; Lecture 7 18

Using Structs with Arrays A nested initialization may be used with an array of structs struct book booklist[2]={ { C by Discovery, Foster,1001.22,776,2005}, { Calculus, Edwards,1401.22,976,1997} }; Lecture 7 19

Using Structs with Pointers A pointer can be declared to reference any data type. struct book * bookptr=null; Lecture 7 20

Using Structs with Pointers A structure can not contain a copy of itself. THIS NEVER WORKS struct node { }; int Data; struct node next; Lecture 7 21

Using Structs with Pointers But a struct can contain an address field. So a structure can contain a pointer to another structure of the same type. struct node { int Data; struct node *next; }; Lecture 7 22

Using Structs with Pointers Assigning an address to a pointer is the same as before. struct book book1; struct book * bookptr=null; bookptr=&book1; Lecture 7 23

Using Structs with Pointers There are two methods of getting the information out of a member of a pointer to a struct. The first is the standard dereference operation * (*bookptr).author Lecture 7 24

Using Structs with Pointers The second is the member selection operation, which is a minus followed by a greater than. -> bookptr->author member selection is a more commonly used notation for referencing the member of a structure pointer. Lecture 7 25

Passing Structures to and from Functions Pointers to structures make them usable to pass to functions. By using pass by reference, the record can be sent to a function, and be processed using the member selection operator. Lecture 7 26

Passing Structures to and from Functions void Example2() { struct book textbook; MakeBook(&textbook,"Embedded C","Micheal Pont",2002,380,123.456); ShowBook(textbook); } void MakeBook(struct book *thisbook,char *title,char *author, int year, long pages, double dewy) { strcpy(thisbook->title, title); strcpy(thisbook->author, author); thisbook->year=year; thisbook->pages=pages; thisbook->dewy=dewy; } Lecture 7 27

Bit Fields A bit field is an feature in C that allows a programmer to assign the contents of a struct to a specific number of bits. This is a technique used in situations when memory is limited on the computer. struct bitset { unsigned int front: 3; unsigned int back: 5; }; Lecture 7 28

Enumerated types An enumerated type is a representation of a series of numbers in C using identifier names. enum tag { identifier_1, identifier_2,..., identifier_n }; This results in a list of identifiers numbered starting from 0 that can be used in place of integers. Lecture 7 29

Enumerated Types An enumerated type can also have a unique integer value explicitly set to the identifier. enum tag { identifier_1=5, identifier_2=3,..., identifier_n=22 }; Lecture 7 30

Enumerator types A declaration of enum boolean {true = 1, false =0}; would be similar to #define false 0 #define true 1 Lecture 7 31

Enumerated types enum days {sun,mon,tue,wed, thu,fri,sat} #define sun 0 #define mon 1 #define tue 2 #define wed 3 #define thu 4 #define fri 5 #define sat 6 Lecture 7 32

Enumerated Types For example, a notation for the days of the week could be: enum days{ sun,mon,tue,wed,thu,fri,sat}; with mon equal to 0 at the beginning of the count. Another example could be the months of summer with an explicit assignment enum summer={jun=6,jul=7,aug=8}; Lecture 7 33

Enumerated Types Enumerated Types are also handy for working a switch statement switch(today) { case mon: printf("monday\n"); break; case tue: printf("tuesday\n"); break; case wed: printf("wednesday\n"); case thu: printf("thursday\n"); break; case fri: printf("friday\n"); break; case sat: case sun: printf("weekend\n"); break; } Lecture 7 34

Enumerated Types Enumerated Types can also be used for handling the indexes of the subscripts of an array. int array[7]={15,16,17,18,19,20,21}; printf("sunday %d\n",array[sun]); printf("monday %d\n",array[mon]); printf("tuesday %d\n",array[tue]); printf("wednesday %d\n",array[wed]); printf("thursday %d\n",array[thu]); printf("friday %d\n",array[fri]); printf("saturday %d\n",array[sat]); Lecture 7 35

Enumerated Types Any integer operation, parameter, or other usage can also have an enumerated type in the same usage. Lecture 7 36

Unions A union is another type of structure that can holds many kinds of data. Unlike Struct, however, a union can only hold one piece of data at a time. union tag { member_type member_name; member_type member_name; } Lecture 7 37

Unions Like struct, the tag is optional for declaring a union, but without a tag variables cannot be created later in the code. Lecture 7 38

Unions This union speedrate contains two different member variables of data. Only one member field of data can be occupied at a time. union speedrate { int miles_per_hour; float km_per_hour; } Lecture 7 39

Unions Unions are accessed and operated upon just like structures. A. operator can be used to get access to a member variable A = operator can be used to assign similar unions. A sizeof() operation can be used to find the size of the union A & operation can be used to find the address of a union Lecture 7 40

Unions Like Structures, Unions can be created as a pointer. As a pointer, the Union can be dereferenced with a * As a pointer, the member variables of the union can be manipulated using a member selection operator -> Lecture 7 41

Unions The main function of a union is to reduce the amount of memory used in a program A struct is the size of its entire member variable list. A union is the size of its largest member variable. Lecture 7 42