Derived types.

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
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.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structures Spring 2013Programming and Data Structure1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Enum, Struct, & Union typedef - a type definition, typedef, gives a name to a data type by creating a new type that can then be used anywhere a type is.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Data Types.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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 Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
+ 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.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
Pointers *, &, array similarities, functions, sizeof.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 8 Arrays, Strings and Pointers
Structures, Unions, Enumerations
11 Chapter Structured Data
Chapter 12 Enumerated, Structure, and Union Types Objectives
Chapter 10-1: Structure.
Pointers, Enum, and Structures
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Introduction to C++.
Module 2 Arrays and strings – example programs.
DATA HANDLING.
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
C Structures, Unions, and Enumerations
Arrays, For loop While loop Do while loop
C Structures, Unions, Bit Manipulations and Enumerations
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Built-In (a.k.a. Native) Types in C++
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 11: Structured Data.
Windows Programming Lecture 04
Classes and Objects.
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Submitted By : Veenu Saini Lecturer (IT)
Chapter 9: Pointers and String
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Standard Version of Starting Out with C++, 4th Edition
Pointers and pointer applications
Structures, Unions, and Enumerations
Presentation transcript:

derived types

Structures and functions The type definition Enumerated types Accessing structures Complex structures Arrays of structures Structures and functions Derived Types

Type Definition (typedef) It gives the name to a data type by creating a new type that can be used anywhere a type is permitted Advantage Allows to replace a complex name such as a pointer declaration with mnemoic that makes the program easier to read and follow typedef type IDENTIFIER ; e.g. typedef int INTEGER; char * stringPtrArray[20]; can be written as typedef char *STRING; STRING stringPtrArray[20]; Derived Types Traditionally upper case Keyword standard or derived type

Enumerated types enum is built on integer types Each integer value is given an identifier called an enumeration constant It allows to use symbolic names rather than numbers which makes our programs much more readable Once enumerated types are defined, we can create variables from standard types C allows the enumerated constants or variables that hold enumerated constants, to be used anywhere that integers can be used Keep enumerated types separate from integer types Derived Types

Enumerated types enum { enumeration constants } variable_identifier; Format 1 : Enumerated variable enum tag { enumeration constants }; enum tag variable_identifier; Format 2: enumerated tag To use multiple enumerated types in a program, you need to create an enumerated type It includes a tag after the keyword enum; the tag is an enum identifier Example of enumeration enum months { jan, feb, mar,april,may}; It is list of one or more identifiers separated by commas Here jan equates to 0, feb to 1 and so on enum months {jan=1, feb , jun=6, aug=1}; enum months birthmonth; Derived Types

Enumerated type definition enum colors{ red, white, blue}; enum colors acolor; Format 1 : Enumerated variable typedef enum{ red,white, blue } COLORS; COLORS aColor; Format 2: Enumerated typedef Derived Types

example /* program to demonstrate the use of enum in switch*/ Main( ) { enum items {laptop=1,cellphone,TV, ironbox}; int choice; printf(“enter your choice”); scanf(“%d”,&choice); switch(choice) case laptop: printf(“ you pressed laptop”); break; case cellphone:printf(“ you pressed cellphone”); case TV: printf(“ you pressed TV”); case ironbox: printf(“ you pressed ironbox”); } Here meaningful names are given in switch instead of number. Derived Types

Structure format variation … } variable_identifier; -------------------------------------------------------------------------------- struct tag { struct tag variable_identifier; ----------------------------------------------------------------------------------------typedef struct { } TYPE_ID; TYPE_ID variable_identifier; Derived Types

Example typedef struct { int x; int y; float t; char u; } SAMPLE; SAMPLE sam1; SAMPLE *ptr; ptr=&sam1; Derived Types

Accessing structures Each field in a structure can be accessed and manipulated using expressions and operators Period (.) is used to distinguish normal identifiers from the members in the structure e.g. If (employee1.gender = = ‘M’ ) employee1.salary +=employee1.hra; scanf(“%d %f %c “, &sam1.empcode, &sam1.salary, &sam1.gender); (.) precedence 17, postfix -16, unary increment -15 sam1.x++ and ++sam2.x are valid Derived Types

Structure operations A structure can only be copied to another structure of the same type using the assignment operator Derived Types

Pointers to structures Define a pointer for the structure SAMPLE *ptr; ptr=&sam1; Structure can be accessed using indirection operator (*) refering to the whole structure E.g. (*ptr ).x Brackets essential because precedence of member operator (17) is higher than indirection operator (15) *ptr.x is interpreted as *(ptr.x) (*pointername ).fieldname same as pointername-> fieldname Derived Types

Complex structure Structure within structure (nested structures), arrays within structures, arrays of structures Nesting must be done from inside out- innermost structure first, then next level, working upward toward the outer most inclusive structure Each structure must be initialized completely before proceeding to the next number Each structure enclosed in the braces Derived Types

Defining arrays for structures Structures can have more than one array as members. They can be accessed either by indexing through pointers as long as they are properly qualified with member operators Like structures an array may be included within the structure or may be declared separately and then included If declared separately, the declaration must be complete before it can be used in the structure Regardless of how we declare the structure, each element will have the same reference. First to the structure and then to the array element When structure contains array we can use pointers to refer directly to the array elements Derived Types

Array initialization in structures Since array is a separate member, its values must be included in a separate set of braces Use of pointers can save memory e.g. char may[]=“May” stamp.date.month=may;// assigns month “May” to the structure Derived Types

Array of structures Create the array as done for normal array of integers E.g. STUDENT stuary[50]; To access the midterm marks of one of the subject 1 for student 4, we can write studAry[4].midterm[1]; The index operator the member operator, the selection operator have same precedence and associativity is from left to right Derived Types

Structures and functions For structures to be useful, we must be able to pass them to functions and return them A function can access the members of a structure in three ways Individual members can be passed to the function Whole structure can be passed and the function can access the member using pass by value The address of a structure or member can be passed, and the function can access the members through indirection and selection operators ( pass by address) We can send the individual elements or the whole structure to a function A function can also return a structure Derived Types

Passing structures through pointers When structures are large, efficiency could suffer, especially with heavily used function You will find that structures are passed through pointers It is common to pass structures thorugh pointers when the structure is in dynamic memory Selection operator () has higher precedence than the address operator(&) and it can be coded without parenthesis e.g. &ptrnumerator &(*pFr).denominator // member operator has higher precedence over indirection operator and address operator Since the address and member operator are the same level, we need to use the parenthesis only around the pointer dereference Derived Types

unions

Member accessing is similar to structures. A union is a construct that allows memory to be shared between different data types. Declaration syntax is similar to that of structure except the keyword struct to be replaced by union. union sharedData{ char chAry[2]; short num; }; Both share the same location i.e chAry[0] is MSB byte of num and chAry[1] is LSB byte of num. Member accessing is similar to structures. Derived Types

Other types can be read or assigned value using assignment operator. Only the first type declared in the union can be initialized while defining union variable. Other types can be read or assigned value using assignment operator. While initializing, values must be enclosed in {} A structure member can be an union. And vice versa. Example Derived Types