Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

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.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structure.
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 C++ Syntax and Semantics The Development Process.
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.
11.3 – Rational Functions. Rational functions – Rational functions – algebraic fraction.
CSE1301 Computer Programming: Lecture 33 Linked Lists.
Encapsulation by Subprograms and Type Definitions
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
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.
1 Structured Data Types A structure can be used to combine data of different types into a single (compound) data value. Like all data types, structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
4.5 Equivalent Fractions. Equivalent Fractions Defined Fractions that name the same amount. Example:
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.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Computer Graphics Lecture 1 July 11, Computer Graphics What do you think of? The term “computer graphics” is a blanket term used to refer to the.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Structured data types An elegant way to deal with the rgb image is to employ structured data types. A Java class is a generalization of a C struct. A structure.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
CPS120: Introduction to Computer Science Lecture 15A Structures.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CPS120: Introduction to Computer Science Data Structures.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
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.
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.
Fractions and Sets 9-3 I can show and understand that fractions are equal parts of a whole. 3.NF.1.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
1 CSE1301 Computer Programming: Where are we now in the CSE1301 syllabus?
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
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.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Find the common denominator. Make equivalent fractions.
Chapter 10-1: Structure.
Chapter 11: Structured Data.
Fractions: Parts of a Set
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Windows Programming Lecture 04
Chapter 1: Introduction to Data Structures(8M)
Structure.
C Structures, Unions, Bit Manipulations and Enumerations
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Structs CSE 2451 Rong Shi

Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group together different types of variables under the same name Compared to arrays – Data members may have different size – Not as easy to access members ( i.e. no [], use dot operator. instead ) – Structure variable names are not constant pointers like array names

Struct definition struct { } ;

Example struct fraction { int numerator; int denominator; }; struct fraction f1, f2 = {.numerator = 1,.denominator = 1}; f1.numerator = 2; f1.denominator = 3; f2 = f1; if(f1 == f2)// invalid, == not defined for structs

Struct names/tags The tag field sets a name to the struct Allows creation of structures of the same type struct PIXEL { float red; float green; float blue; } ; struct PIXEL p; struct PIXEL image[20], *imp;

Example of struct data members struct DATAMEMBERS { float f;// single variable int a[20];// array long *lp;// pointer struct PIXEL pix;// a different struct struct PIXEL image[10];// array of different structs struct PIXEL *imp;// pointer to a different struct } single, array [10];

Struct data member access Dot operator (. ) – for variables – struct_variable_name.member_name – Ex: single.f array[2].image[10].red Indirection ( -> ) – for pointers – Pointer_to_struct->member_name – Ex: struct DATAMEMBERS * s = &single; s->f Note: (*s).f == s->f

typedefs and structs typedef ; typedef struct { float red; float green; float blue; } Pixel; Pixel p; Pixel image[100], *imp; (Avoids the need to type struct in front of struct name)

Struct members of the same type struct SELF_REF1 { int a; struct SELF_REF1 b; }; Struct SELF_REF2 { int a; struct SELF_REF2 *b; };