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.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Advertisements

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
Structures Spring 2013Programming and Data Structure1.
Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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.
Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Data Types.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Lecture 8. Lecture 8: Outline Structures [Kochan, chap 9] –Defining and using Structures –Functions and Structures –Initializing Structures. Compound.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Structured Data Types array array union union struct struct class class.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 8 Structures, Unions,
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Structures and Unions in C Alan L. Cox
© Oxford University Press All rights reserved. CHAPTER 8 STRUCTURES.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
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.
COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists.
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 *
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.
‘C’ Programming Structures and Commands
Structures, Unions, Enumerations
Structure, Unions, typedef and enumeration
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures putting data together.
1. Structures 2. Abstract Data Types
Structures and Union.
Derived types.
Windows Programming Lecture 04
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 1: Introduction to Data Structures(8M)
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Structures Chapter 4.
Structures, Unions, and Enumerations
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

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 { type data_member1; type data_member2; …; type data_memberN; };

Why Structure To collect a set of variables together, and reference them as a unit. –Group different type of variables –Treat them as a single object

Declaration Three ways struct date{ int day; char month[10]; int year; }; struct date today; typedef struct { int day; char month[3]; int year; } date; date today; struct { int day; char month[3]; int year; } today;

Initialization struct { int day; char month[10]; int year; } today = {19, “ March ”, 2007}; typedef struct { int day; char month[4]; int year; } date; date today = {19, “March”, 2007}; struct date{ int day; char month[10]; int year; }; struct date today = {19, “March”, 2007};

How to use To access the members in the structure –specify the variable name, followed by a period and the member name today.day = 19; today.year = 2007; today.month[0]=‘M’; today.month[1]=‘a’; today.month[2]=‘r’; today.month[3]=‘c’; today.month[3]=‘h’; today.month[3]=‘\0’; 19 M a r c h \ month.day.year today

How to use structure – cont. Structure variable can be passed as a parameter to a function. Structure variable can be returned from a function Can have arrays of structures, and structures containing arrays or other structures. –struct date list_of_days[10]; –struct diary { int location; struct date when; char names[10]; };

Scope A structure type declaration can be local or global –if declared locally, it is only valid locally. Example: void main () { struct ONE { int a; float b; }; struct ONE x; } int f() { struct ONE x; }

union union is a collection of variables of different types Similar to a structure, except only one field of information can be stored at any time union union_name { type data_member1; type data_member2; …; type data_memberN; };

Example union length { int foot; int meter; }; union length obj; obj.foot = 3; obj.meter = 5; printf("The object size is %i foot ", obj.foot); printf("and %i meter \n.", obj.meter); The object size is 5 foot and 5 meter.

Comparing to Structure struct length { int foot; int meter; }; struct length obj; obj.foot = 3; obj.meter = 5; printf("The object size is %i foot ", obj.foot); printf("and %i meter \n.", obj.meter); The object size is 3 foot and 5 meter.

enum enum: enumerate –declare and initialize a sequence of integer constants enum colors {RED, YELLOW, GREEN, BLUE}; enum colors {RED=5, YELLOW, GREEN, BLUE}; Delaration –enum colors background; Assign value –background = RED;

Integer Value for Enum #include int main() { enum colors {RED, YELLOW, GREEN, BLUE}; printf("RED = %d\n", RED); printf("YELLOW = %d\n", YELLOW); printf("GREEN = %d\n", GREEN); printf("BLUE = %d\n", BLUE); return 0; } RED = 0 YELLOW = 1 GREEN = 2 BLUE = 3