Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
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.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Types in C. Data Transformation Programs transform data from one form to another –Input data  Output data –Stimulus  Response Programming languages.
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.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Pointer Data Type and Pointer Variables
Advanced Data types and Sorting
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
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.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
+ 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 Data Structures.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
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.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Classes C++ representation of an object
Chapter 10-1: Structure.
Pointers, Enum, and Structures
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Student Book An Introduction
Using local variable without initialization is an error.
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Classes and Objects.
Chapter 1: Introduction to Data Structures(8M)
C Programming Pointers
Classes C++ representation of an object
Programming Fundamental
Presentation transcript:

Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld

Structures Structures are collections of related variables under one name Derived data type – i.e. constructed using objects of other types Unlike arrays, structures can store variables of many different types (heterogeneous and not homogeneous)

Parts of the structure Keyword struct introduces the structure Structure tag names the structure definition –Used with keyword struct when you declare variables of the structure type Members are the variables declared within the structure definition –Must have unique names within the structure definition –In databases, called “fields”

structure definitions struct student { char nameFirst[10]; char nameLast[10]; int firstExam; int secondExam; int final; }; keyword struct: tells the compiler you are defining a struct structure tag: used to name the structure definition members of the structure don't forget the semi-colon

structure definition (cont’d) The structure definition makes a new type It does not allocate space in memory to store variables The structure is another example of a complex (or derived) data type

members A struct’s members can be simple data types (int, char, etc) or complex data types (arrays, other structs) No two members of a struct can share the same name A struct cannot contain another instance of itself (you need to use a pointer for that)

declaring struct variables Once the struct is defined, we can declare variables of our new type. struct student currentStudent, class[50]; keyword struct array to hold 50 student records struct tag variable to hold one student record

struct comparison You cannot compare structs using == or != –trying to do so should cause a syntax error –same problems as arrays Instead you must write code which will compare the structs

Initializing structs The syntax for initializing structs is similar to the syntax for arrays. struct student currentStudent = { “ Bart ”, “ Simpson ”, 55, 64}; Declares the variable currentStudent and initializes nameFirst, nameLast, firstExam, secondExam with the values provided. final would be initialized to zero because no value was specified.

Accessing members of a struct We use the struct member operator (dot operator) to access members of a struct (there is another method which uses pointers) currentStudent.firstExam would reference the value stored in the variable currentStudent, member firstExam You can use this notation to reference a struct’s values any place you can use other variables (including function parameters)

Passing structs to functions You can pass entire structs to function. Unlike arrays, a struct is passed to a function using call by value (in other words, if you change the value of the struct in a function, that change will not be reflected in the calling function)

Makin’ your own variable types C provides a mechanism through which you can simplify struct usage and enable you to use it as you would another type Use the keyword typedef

typedef The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. The statement: typedef struct student TStudent; defines a new type “TStudent” as a synonym for type struct student. Once the typedef synonym is defined, you can declare your variables like this: TStudent class[ 50 ] ;

typedef (cont’d) It is good practice to capitalize the first letter of typedef synonym. It is better practice to preface it with a capital “T” to indicate it’s one of your new types e.g. –TRobot, TStudent, etc.

Simple Struct Program

An Array of Structs

Structs with functions