Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
C Language.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Dale/Weems/Headington
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
1 C++ Syntax and Semantics, and the Program Development Process.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
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:
Array, Structure and Union
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
LESSON 3 IO, Variables and Operators
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Review for Final Exam.
Pointer to Structures Lesson xx
Chapter 1: Introduction to Data Structures(8M)
Review for Final Exam.
C Programming Lecture-8 Pointers and Memory Management
C Structures and Memory Allocation
Presentation transcript:

Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual items that comprise a structure can be any of the basic data types such as char, int, float,and double. They can also include arrays or even other structures. Structures enable us to build our own data types and to group data of different types under a common name. To create a structure you must: 1. Define the structure. 2. Declare variables of that type.

11.1 The Structure Template As an example of the syntax for defining a structure, consider the following structure definition. struct rectangle { float x1; float y2; int color; int id_number; int level; };

Declaring Structures To create structure variable, we have to declare variable of the type defined in the structure definition. For example, to create variable of the type rectangle, we use the following declaration struct rectangle old_rect, curr_rect, new_rect; This declaration creates three variable, old_rect, curr_rect, and new_rect, of type rectangle. It also allocates storage for each of these variables.

It is possible to define a structure and declare variables of the specified structure type in a single statement as in the following. struct rectangle { float x1; float y2; int color; int id_number; int level; } old_rect, curr_rect, new_rect;

Accessing Structure Members A special syntax in needed for accessing the members of a structure. A member of a structure is accessed by specifying the name of the structure variable, followed by the structure member of operator (.) and the member name. structure_variable. member_name Thus the members of the structure variable new_rect of the structure rectangle can be accessed using the following expressions. new_rect.x1; new_rect.y1; new_rect.color; new_rect.id_number; new_rect.level;

Example: Accessing Structure Members #include struct student_record { char last_name[41]; char first_name[21]; } s41g0001; void main(void) { strcpy(s41g0001.first_name, “John”); strcpy(s41g0001.last_name, “Antony”); printf(“\n First Name is : %s”, s41g0001.first_name); printf(“\n Last Name is : %s”, s41g0001, last_name); }

Program Output: First Name is : John Last Name is : Antony 11.2 The typedef Statement The typedef statement is used to create synonyms for data types. These data types can be the built-in C data types such as char, int, float, and double or any other user defined data types. Suppose we want to use the identifier REAL to represent variables of type float. We can do this using the following typedef statement typedef float REAL;

This statement defines the name REAL to be a synonym for float. We can now use the name REAL in our programs instead of float. For example, we can declare variables using the statement REAL a, b, c; This statement is equivalent to float a, b, c; The general syntax for the typedef statement is typedef

11.3 Unions C provides a variable type called a union that allows a variable to store values of different types with thr restriction that only one value can occupy the variable at any given time. Unions provide a way to economize on storage. The syntax for creating and using unions is the same as for structures. We first create a union template with an optional tag and then use this tag for creating union variables. The following code declares a union. Union input_value { float real_num; int num; char letter; } input1;

11.4 Enumerated Types Please read some information about C enumerated data type from the book.

Any Questions