+ 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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
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.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
VBA Modules, Functions, Variables, and Constants
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
Chapter 11: Classes and Data Abstraction
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Combining data types into a logical groupings.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Week 12 Methods for passing actual parameters to formal parameters.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Programming Fundamentals Enumerations and Functions.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 10-1: Structure.
Computer Programming BCT 1113
Java Primer 1: Types, Classes and Operators
TMF1414 Introduction to Programming
Introduction to C++.
JavaScript: Functions.
Java Review: Reference Types
User-Defined Functions
Lecture 18 Arrays and Pointer Arithmetic
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Chapter 9: Pointers and String
Structures, Unions, and Enumerations
Programming Fundamental
Presentation transcript:

+ 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 int of float. What if we want array of Time: seconds, minutes, hours Data: day, month, year Book: author, title, price, year City: name, country, population Address: name, door-number, street, city Inventory: item, stock, value Customer: name, telephone, city, category C supports a constructed data type known as structures. A mechanism for packing data of different types.

+ Defining a structure Structures must be defined first for their format that may be used later to declare structure variables. Consider a book database consisting of book name, author, number of pages, and price. struct declares a structure to hold titles, author, pages and price. array of 20 characters array of 15 characters integer float title author pages price

+ General format of a structure The template is terminated with a semicolon. While the entire definition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template. The tag name such as book_bank can be used to declare structure variables of its type, later in the program. structtag_name { data_typemember1; data_typemember2; };

+ Declaring Structure Variables The structure declaration includes the following elements: The keyword struct. The structure tag name. List of variable names separated by commas. A terminating semicolon.

+ Arrays vs Structure Both the arrays and structures are classified as structured data types as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they different in a number ways. An array is a collection of related data elements of same type. Structure can have elements of different types. An array is derived data types whereas a structure is a programmer-defined one. Any array behaves like a built-in data types. In the case of a structure, first we have to design and declare a data structure before the variables of that type are declared and used.

+ Accessing structure members The link between a member and a variable is established using the member operator ‘.’ (also know as ‘dot operator’). book1.price Assigning examples strcpy(boo1.title, ‘BASIC’); strcpy(book1.author, ‘John S’); book1.pages = 250; book1.price = ;

+ Structure Initialization Like any other data type, a structure variable can be initialized at compile time. The following statements initialize two structure variables.

+ Structure Initialization C language doe not permit the initialization of individual structure members within the template. The initialization must be done only in the declaration of the actual variables. The compile-time initialization of a structure variable must have the following elements: The keyword struct. The structure tag name. The name of the variable to be declared. The assignment operator =. A set of values for the members of the structure variable., separated by commas and enclosed in braces.

+ Rules for initializing structures We cannot initialize individual members inside the structure template. The order of values enclose in braces must match the order of members in the structure definition. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list. The uninitialized members will be assigned default values as follows: Zero for integer and floating numbers. ‘\0’ for characters and strings

+ Copying and comparing structure variables Two variables of the same structure type can be copied the same way as ordinary variables. Person1 = Person2; Person2 = Person1; However, the statements such as Person1 == Person2 Person1 != Person2 Are not permitted.

+ Copying and comparing structure variables The program illustrate the comparison of structure variables

+ Word boundaries and slack bytes Computer stores structures using the concept of “word boundary”. The size of a word boundary is machine dependent. In a computer with two bytes word boundary, the member of a structure a stored left_aligned on the word boundary charint slack byte Undefined value Because of undefined values we cannot compare structures.

+ Operations on individual membes A member with a dot operator along with its structure variable can be treated like any other variable name and therefore can be manipulated using expressions and operators. We also apply increment and decrement operators to numeric type members. student1.number++; ++student1.number;

+ Three ways to access members We have used the dot operator to access the number of structures variables. There are two other ways: The identifier ptr is know as pointer that has been assigned the address of the structure variable v. Now, the members can be accessed in three ways: using dot notation: v.xx using indirection notation: (*ptr).x Using selection notation: ptr  x

+ Arrays of structures To declare an array of structures, the following syntax is used: struct class student[100]; each element of the array represents a structure variable. Consider the following declaration: An array of structures is stored in the memory in the same way as a multi-dimensional array student[0] student[1]student[2]

+ Example The program calculates the subject-wise and student-wise total and store them as a part of the structure.

+ Arrays with structures C permits the use of single or multidimensional arrays as structure members. The member subject contains three elements, subject[0], subject[1], subject[2]; These elements can be accessed using appropriate subscripts..

+ Arrays with structures Example

+ Structures within structures Structures within a structure means nesting of structures. This structure define name, department, basic pay and three kinds of allowances. Lets group them together.

+ Structures within structures The following declaration is legal We can also use tag names

+ Structures and functions Structures can be transferred from one function to another by Passing each member of the structure as an actually argument; Passing of a copy of the structure, any changes to structure members are not reflected in the original structure. Using pointer to a structure. The address location of the structure is passed to the called function.

+ Structures and functions The general format of sending a copy of a structure to the called function is function_name (structure_variable_name); The called function take the following format data_type function_name(struct_type st_name){ … return (expression); } data_type function_name(struct_type st_name){ … return (expression); }

+ Structures and functions The following points are important to note: The called function must be declared for its type, appropriate to the data type it is expected to return. The structure variable used as the actual argument and the corresponding formal argument in the called function must be of the same struct type. The return statement is necessary only when the function is returning some data back to the calling function. The expression may be any simple variable or structure variable. When a function returns a structure, it must be assigned to a structure of identical type in the calling function. The called functions must be declared in the calling function appropriately.

+ Structures and functions The function update receives a copy of the structure variable item as one of its parameters. Note that both the function update and the formal parameter product are declared as type structure stores. The function mul is of type float because it returns the product of price and quantity.

+ Pointers and structures Suppose product is an array variable of struct type. The name product represents the address of its zeroth element. The statement declares product as an array of two elements, each of the type struct inventory and ptr as a pointer to data objects of the type struct inventory. The assignment Would assign the address of the zeroth element of product to ptr.

+ Pointers and structures

+ Passing structures by address Previous methods of passing structures are inefficient in termos of both, the execution speed and memory. To overcome this drawback we pass a pointer to the structure and then use this pointer to work on the structure members.

+ Unions Union is a concept borrow from structures and therefore follow the same syntax as structures. The major distinction between them is in terms of storage. The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union. Byte 1Byte 2Byte 3Byte To access a union member, we can use the same syntax as for struct.