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.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Programming in C Chapter 10 Structures and Unions
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
C Language.
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.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if your program had an array to store info regarding.
Objective Revision of data structures and their implementation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
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.
LECTURE 4: INFORMATION REPRESENTATION (PART II) COMP26120: ALGORITHMS AND IMPERATIVE PROGRAMMING.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
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.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Functions, Pointers, Structures Keerthi Nelaturu.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Prepared By Ms.R.K.Dharme Head Computer Department.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
Copyright © Curt Hill Structured Data What this course is about.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Data Structures Lecture 1: Introduction. Course Contents Data Types   Overview, Introductory concepts   Data Types, meaning and implementation  
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.
Array, Structure and Union
Structures Combining data types into a logical groupings.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
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.
+ 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.
CPS120: Introduction to Computer Science Lecture 15A Structures.
CPS120: Introduction to Computer Science Data Structures.
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.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
chap11 Chapter 11 Structure and Union Types.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
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)
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Programming Structures.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Structures.
Chapter 1: Introduction to Data Structures(8M)
Structure and Union Types
C Programming Lecture-13 Structures
Structures.
Chapter 10 C Structures and Unions
Chapter 11 Structure and Union Types.
Structures Declarations CSCI 230
Presentation transcript:

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 wanted to store personnel information like name, ID, address and SSN, all of different types then using arrays will be impractical Structures allow us to combine variables of different types with a single name A convenient way of grouping several pieces of related information together

Structures In addition to the primitive data types such as int, char, float, structures can have members that are other structures, arrays or pointers Can get complicated however.

Defining Structures A structure type is usually defined using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

Defining Structures General syntax for defining structures is: typedef struct { ;.... ; } ;

Defining Structures The following is a definition of a new type typedef struct { char name[64]; char course[128]; int year; } student_info; variables of type student_info can be declared as follows. student_info student_1; // same as declaring int year; // all structures declared as of type student_info will have members // name, course and year

Accessing members of structures All members of a structure behave exactly the way they would if they were being used independently However, to access members of a structure we have to use the. (dot) operator Consider the above declaration: student_info student_1; To assign values to its members, the syntax is: strcpy(student_1.name, “John”); strcpy(student_1.course, “COP 3223H”); student_1.year = 2014;

Example - Accessing members of structures Write a program to declare a new data type called student_info using the struct and typedef keywords. The struct student_info should have student’s name, course and graduation year as members. Ask the user to input values and store them in appropriate members of a student_info struct

Structures – Representing date Write a program to declare a new data type called date to store year, month and day using the struct and typedef keywords.

Example – Structures within structures Write a new version of the program to declare a new data type called date to store a student’s name, course and graduation date. The date member in this program is the date structure created in the previous example

Other Example of structures Complex numbers have two parts, a real value and an imaginary part. Structures are best suited for these Act as databases e.g. an array of structures would hold all relevant information of all students in a course Other data structures like trees, graphs, heaps, stacks and queues make use of structures Towards the end of our course, we will study linked lists that make use of structures

Arrays of structures Just as arrays for primitive data types, we can have arrays of structures E.g., an array of student_info structures would be declared as: student_info student[3]; Elements of the struct array are accessed by specifying their index locations and the. (dot) operator: scanf(“%s”, &student[0].name);

Arrays of structures A visual representation of an array of structures name course grad_year Student[0] name course grad_year Student[1] name course grad_year Student[2]