Programming for Engineers Structures, Unions

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

Fundamentals of Programming Session 24

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

OBJECT ORIENTED PROGRAMMING USING C++

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

Fundamental of Programming (C)

Chapter 10 C Structures, Unions, Bit Manipulations

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

C Structures, Unions, Bit Manipulations, and Enumerations

History. used in early Mac development notable systems in Pascal Skype TeX embedded systems

High Performance Computing

Syntax and Variables

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Data Types H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 17, 2018) Washington State University

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

Recursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.

software.sci.utah.edu (Select Visitors)

Types, Operators and Expressions

Types, Operators and Expressions

HPE Security Data Security. HPE SecureData. Product Lifecycle Status. End of Support Dates. Date: April 20, 2017 Version:

Arrays III and Enumerated Types

This report is based on sampled data. Jun 1 Jul 6 Aug 10 Sep 14 Oct 19 Nov 23 Dec 28 Feb 1 Mar 8 Apr 12 May 17 Ju

IV Unit Second Part STRUCTURES

CMIS 102 Hands-On Lab

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

File Handling in C. EECS 2031 Fall October 27, 2014

Data Representation and Storage

Lecture 14. Dynamic Memory Allocation

Section 1.2: What is a Function? y = 4x

Systems Programming. 05. Structures & Trees. Alexander Holupirek

CS Programming I: Arrays

Asks for clarification of whether a GOP must communicate to a TOP that a generator is in manual mode (no AVR) during start up or shut down.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

Computer Systems Principles. C Pointers

Chapter 8 STRUCTURES IN C

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

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

Scientific Programming in C X. More features & Fortran interface

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

ICT PROFESSIONAL MICROSOFT OFFICE SCHEDULE MIDRAND

More BSTs & AVL Trees bstdelete

Excel Functions & Tables

12 CREATING NEW TYPES

CA341 - Comparative Programming Languages

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

Data Representation and Storage. Some definitions (in C)

SCI - software.sci.utah.edu (Select Visitors)

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

2

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

2

2

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

C Programming for Engineers Functions

Fundamentals of Programming Session 25

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

User Defined data Types

Types and Structures

Programming for Engineers Pointers

EEE145 Computer Programming

CSE 230 Intermediate Programming in C and C++

Database Programming with SQL

Type Checking. Prof. James L. Frankel Harvard University

Structures Unions and Enumerated Datatypes 224

Functions and Recursion

CS1100 Introduction to Programming

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

C Programming for Engineers Structured Program

Chapter 14. Structures

Principles of Programming Languages

Polycom Advantage Service Endpoint Utilization Report

COMP322 - Introduction to C++

Polycom Advantage Service Endpoint Utilization Report

All King County Summary Report

Data Structures Unit 02

Exam 2 ITEC 120 Principles of Computer Science I Spring: 2017

C Language, Token, Keywords, Constant, variable

Computer System and programming in C

FUNCTION POINTERS. int (*pf)(); // pf is a pointer to a function returning an int.

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Fluidity Trader Historical Data for Ensign Software Playback

Solved Exercises from exams: Midterm(1)

Monthly SEO Report. Example Client 16 November 2012 Scott Lawson. Date. Prepared by

September Real Sector Statistics Division. Methodology

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

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Programming. Structures, enums and unions

Programming for Engineers Arrays

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

Seattle (NWMLS Areas: 140, 380, 385, 390, 700, 701, 705, 710) Summary

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Transcription:

Programming for Engineers Structures, Unions ICEN 200 Spring 2017 Prof. Dola Saha 1

Structure Ø Collections of related variables under one name. Ø Variables of may be of different data types. Ø struct card { char *face; char *suit; }; Ø Keyword struct introduces the structure definition. Ø Members of the same structure type must have unique names, but two different structure types may contain members of the same name without conflict. Tag Members 2

Structure Declaration Ø Ø Ø struct employee { char firstname[20]; char lastname[20]; unsigned int age; char gender; double hourlysalary; }; struct employee employee1, employee2; struct employee employees[100]; Ø struct employee { char firstname[20]; char lastname[20]; unsigned int age; char gender; double hourlysalary; } employee1, employee2, *employeeptr; 3

Structure Tag Ø The structure tag name is optional. Ø If a structure definition does not contain a structure tag name, variables of the structure type may be declared only in the structure definition not in a separate declaration. 4

Self Reference Ø Ø Ø Ø Ø A structure cannot contain an instance of itself. A variable of type struct employee cannot be declared in the definition for struct employee. A pointer to struct employee, may be included. For example, o struct employee2 { char firstname[20]; char lastname[20]; unsigned int age; char gender; double hourlysalary; struct employee2 person; // ERROR struct employee2 *eptr; // pointer }; struct employee2 contains an instance of itself (person), which is an error. 5

Storage in Memory Ø Structures may not be compared using operators == and!=, because structure members are not necessarily stored in consecutive bytes of memory. Ø Computers may store specific data types only on certain memory boundaries such as half-word, word or doubleword boundaries. Ø A word is a standard memory unit used to store data in a computer usually 2 bytes or 4 bytes. 6

Storage in Memory Ø struct example { char c; int i; } sample1, sample2; Possible storage, but machine dependant 7

Initialization Ø struct card { char *face; char *suit; }; Ø struct card acard = {"Three", "Hearts"}; Ø If there are fewer initializers in the list than members in the structure, the remaining members are automatically initialized to 0 or NULL if the member is a pointer. Ø Assignment Statement of same struct type struct card acard1 = acard2; 8

Accessing Structure Members Ø the structure member operator (.) also called the dot operator printf("%s", acard.suit); // displays Hearts Ø the structure pointer operator (->) also called the arrow operator. cardptr = &acard; printf("%s", cardptr->suit); // displays Hearts Following are equivalent o cardptr->suit o (*cardptr).suit 9

Example 10

Structure with Function Ø Structures may be passed to functions by passing individual structure members by passing an entire structure by passing a pointer to a structure. Ø Functions can return individual structure members an entire structure a pointer to a structure 11

typedef Ø Ø Ø Ø The keyword typedef is a way to create synonyms (or aliases) for previously defined data types. Names for structure types are often defined with typedef to create shorter type names. Example: typedef struct card Card; Card is a synonym for type struct card. Example: typedef struct { char *face; char *suit; } Card; Card mycard, *mycardptr, deck[52]; 12

Card Shuffling Example (1) 13

Card Shuffling Example (2) 14

Card Shuffling Example (3) 15

Card Shuffling Example (4) 16

Card Shuffling Example (5) 17

Structure nested within another structure struct customer { char lastname[ 15 ]; char firstname[ 15 ]; unsigned int customernumber; struct { char phonenumber[ 11 ]; char address[ 50 ]; char city[ 15 ]; char state[ 3 ]; char zipcode[ 6 ]; } personal; } customerrecord, *customerptr; customerptr = &customerrecord; 18

Union Ø A union is a derived data type like a structure with members that share the same storage space. Ø For different situations in a program, some variables may not be relevant, but other variables are so a union shares the space instead of wasting storage on variables that are not being used. Ø The members of a union can be of any data type. Ø The number of bytes used to store a union must be at least enough to hold the largest member. 19

Definition Ø union number { int x; double y; }; Ø In a declaration, a union may be initialized with a value of the same type as the first union member. Ø union number value = {10}; Ø union number value = {1.43}; // ERROR 20

Permitted Operations Ø The operations that can be performed on a union are: assigning a union to another union of the same type, taking the address (&) of a union variable, and accessing union members using the structure member operator and the structure pointer operator. Ø Unions may not be compared using operators == and!= for the same reasons that structures cannot be compared. 21

Union Example (1) 22

Union Example (2) 23

Union Use Case typedef union { int wears_wig; char color[20]; } hair_t; 24

Structure & Union Example (1) 25

Structure & Union Example (2) 26

Structure & Union Example (3) 27

Structure & Union Example (4) 28

Structure & Union Example (5) 29

Structure & Union Example (6) 30

Structure & Union Example (7) 31

Enumeration Ø Ø Ø Keyword enum, is a set of integer enumeration constants represented by identifiers. Values in an enum start with 0, unless specified otherwise, and are incremented by 1. For example, the enumeration o enum months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; creates a new type, enum months, identifiers are set to the integers 0 to 11, respectively. Ø Example: enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; identifiers are set to integers 1 to 12, respectively. 32

Enumeration Example 33

Enumeration Example Output 34

Enumerated Data Example 35