Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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.
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.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + malloc +
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
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.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
C and Data Structures Baojian Hua
Encapsulation by Subprograms and Type Definitions
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Learners Support Publications Classes and Objects.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
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.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
Today’s Material Aggregate Data Types: Structures and Unions
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
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.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
Structures and Unions in C Alan L. Cox
+ 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.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Engineering Computing I Chapter 5 Pointers and Arrays.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
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.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
D. GotsevaPL-Lectures1 Programming Languages Lectures Assoc. Prof. PhD Daniela Gotseva
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)
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Chapter 12 Enumerated, Structure, and Union Types Objectives
Chapter 10-1: Structure.
Pointers, Enum, and Structures
TMF1414 Introduction to Programming
CS150 Introduction to Computer Science 1
CSC 253 Lecture 8.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, Bit Manipulations and Enumerations
CSC 253 Lecture 8.
Classes and Objects.
Structures, Unions, and Typedefs
CS111 Computer Programming
Structures, Unions, and Enumerations
Presentation transcript:

Engineering Computing I Chapter 6 Structures

Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.  Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities Spring 20112Chapter 6

Examples of Structures payroll record: o address address 1 address 2 city State Zip code o social security number o salary base salary overtime o social security number Spring 20113Chapter 6

Basics of Structures declaration structure tag member struct { int x; int y; } x, y, z; struct point pt; Spring 20114Chapter 6

Referring to a member of a structure structure-name.member printf("%d,%d", pt.x, pt.y); dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y); Spring 20115Chapter 6

Nested Structures Spring 20116Chapter 6

Structures and Functions legal operations on a structure  copying it o passing arguments to functions o and returning values from functions  assigning to it as a unit  taking its address with “&”  accessing its members  Structures may not be compared!  A structure may be initialized by a list of constant member values Spring 20117Chapter 6

Example A function returns a structure Spring 20118Chapter 6

Arrays of Structures Consider writing a program to count the occurrences of each C keyword Spring 20119Chapter 6

Pointers to Structures To Be enhanced by simpler examples Spring Chapter 6

Typedef C provides a facility called typedef for creating new data type names Spring Chapter 6

Typedef Spring Chapter 6

Unions A union  is a variable that may hold (at different times) objects of different types and sizes  the compiler keeps track of size and alignment requirements.  Unions provide a way to manipulate different kinds of data in a single area of storage,  without embedding any machine-dependent information in the program Spring Chapter 6

Unions Syntactically, members of a union are accessed as union-name.member or union-pointer->member Example /simpler Spring Chapter 6

Bit-fields When to Use Bit-fields? storage space is at a premium one common use is a set of single-bit flags in applications like compiler symbol tables hardware devices registers, also often require the ability to get at pieces of a word Spring Chapter 6

Bit-fields turns on the EXTERNAL and STATIC bits in flags turns them off is true if both bits are off Spring Chapter 6

Bit-fields turns the bits on turns them off Tests the bits Spring Chapter 6