STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Programming Lecture 23 Enumeration Types Structures.
Programming in C Chapter 10 Structures and Unions
Data Structures Using C++ 2E
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.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Structure.
Structures Spring 2013Programming and Data Structure1.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
© 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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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:
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
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.
 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 Types Declarations Expressions Data storage C++ Basics.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
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.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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)
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Windows Programming Lecture 03. Pointers and Arrays.
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.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
Chapter 10: Pointers 1.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Learning Objectives Structures Structure types
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
C Structures, Unions, Bit Manipulations and Enumerations
Built-In (a.k.a. Native) Types in C++
CS148 Introduction to Programming II
Classes and Objects.
CS111 Computer Programming
Data Structures and Algorithms Introduction to Pointers
Programming Fundamental
Presentation transcript:

STRUCTURES

C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various types  like the notion of class/field in C or C++ – but without the data hiding features  scalar in that C treats each structure as a unit  as opposed to the “array” approach: a pointer to a collection of members in memory  entire structures (not just pointers to structures) may be passed as function arguments, assigned to variables, etc.  Interestingly, they cannot be compared using == (rationale: too inefficient)

Structure declarations Combined variable and type declaration struct tag {member-list} variable-list; Any one of the three portions can be omitted struct {int a, b; char *p;} x, y; /* omit tag */ – variables x, y declared with members as described: int members a, b and char pointer p. – x and y have same type, but differ from all others – even if there is another declaration: struct {int a, b; char *p;} z; /* z has different type from x, y */

Structure declarations struct S {int a, b; char *p;}; /* omit variables */ No variables are declared, but there is now a type struct S that can be referred to later struct S z; /* omit members */ – Given an earlier declaration of struct S, this declares a variable of that type typedef struct {int a, b; char *p;} S; /* omit both tag and variables */ – This creates a simple type name S (more convenient than struct S )

Recursively defined structures Obviously, you can’t have a structure that contains an instance of itself as a member – such a data item would be infinitely large But within a structure you can refer to structures of the same type, via pointers struct TREENODE { char *label; struct TREENODE *leftchild, *rightchild; }

Recursively defined structures When two structures refer to each other, one must be declared in incomplete (prototype) fashion struct HUMAN; struct PET { char name[NAME_LIMIT]; char species[NAME_LIMIT]; struct HUMAN *owner; } fido = { ″ Fido ″, ″ Canis lupus familiaris ″ }; struct HUMAN { char name[NAME_LIMIT]; struct PET pets[PET_LIMIT]; } sam = { ″ Sam ″, {fido}}; We can’t initialize the owner member at this point, since it hasn’t been declared yet

Direct access operator s.m – subscript and dot operators have same precedence and associate left-to-right, so we don’t need parentheses for sam.pets[0].species Indirect access s->m : equivalent to (*s).m – Dereference a pointer to a structure, then return a member of that structure – Dot operator has higher precedence than indirection operator, so parentheses are needed in (*s).m (*fido.owner).name or fido.owner->name Member access. evaluated first: access owner member * evaluated next: dereference pointer to HUMAN. and -> have equal precedence and associate left-to-right

struct COST { int amount; char currency_type[2]; } struct PART { char id[2]; struct COST cost; int num_avail; } layout of struct PART : Here, the system uses 4-byte alignment of integers, so amount and num_avail must be aligned Four bytes wasted for each structure! Memory layout idamountnum_avail cost currency_type

A better alternative (from a space perspective): struct COST { int amount; char currency_type; } struct PART { struct COST cost; char id[2]; int num_avail; } Memory layout id amount num_avail cost currency_type