Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.

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.
© 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.
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.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
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.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Programming Languages -1 (Introduction to C) structures Instructor: M.Fatih AMASYALI
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Dale Roberts, Lecturer Computer Science, IUPUI.
 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.
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
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.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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:
Programming Languages -1 (Introduction to C) structures
Array, Structure and Union
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
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.
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.
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 Lecture 15A Structures.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
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.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
File Processing (Cont.) and Structures
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Pointer to Structures Lesson xx
Chapter: 7-12 Final exam review.
C Structures, Unions, Bit Manipulations and Enumerations
Data Structures and Algorithms Introduction to Pointers
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Getting Started With Coding
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations

Dale Roberts Introduction Structures A collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and trees Example: struct card { char *face; char *face; char *suit; }; char *suit; }; struct introduces the definition for structure card struct introduces the definition for structure card card is the structure name and is used to declare variables of the structure type card is the structure name and is used to declare variables of the structure type card contains two members of type char * card contains two members of type char * These members are face and suit

Dale Roberts Structure Definitions Example: A date consists of several parts, such as the day, month, and year, and the day of the year, and the month name struct date { int day; int month; int year; int year_date; char month_name[4]; }; date : the name of the structure, called structure tag. date : the name of the structure, called structure tag. day, month, …: the elements or variables mentioned in a structure are called members. day, month, …: the elements or variables mentioned in a structure are called members. struct information A struct cannot contain an instance of itself Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory Instead creates a new data type used to declare structure variables

Dale Roberts Declaration of Variables of Structure Declarations method 1:declared like other variables: declare tag first, and then declare variable. struct card { char *face; char *suit; }; struct card oneCard, deck[ 52 ], *cPtr; *cPtr; method 2:A list of variables can be declared after the right brace and use comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; method 3:Declare only variables. struct { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr; struct date { }; struct date d1, d2, d3, d4, d5; struct date { } d1, d2, d3; struct date d4, d5; struct { } d1, d2, d3, d4, d5;

Dale Roberts Structure Definitions Valid Operations Assigning a structure to a structure of the same type Taking the address ( & ) of a structure Accessing the members of a structure Using the sizeof operator to determine the size of a structure Initialization of Structures Initializer lists Example: struct card oneCard = { "Three", "Hearts" }; struct card oneCard = { "Three", "Hearts" }; Example: struct date d1 = {4, 7, 1776, 186, “Jul”}; struct date d2 = {4, 7, 1776, 186, {‘J’,’u’,’l’,’\0’}}; Assignment statements Example: struct card threeHearts = oneCard;

Dale Roberts Accessing Members of Structures Accessing structure members Dot (. ) is a member operator used with structure variables Syntax: structure_name.member struct card myCard; struct card myCard; printf( "%s", myCard.suit ); printf( "%s", myCard.suit ); One could also declare and initialize threeHearts as follows: struct card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”; Arrow operator ( -> ) used with pointers to structure variables struct card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); myCardPtr->suit is equivalent to (*myCardPtr).suit myCardPtr->suit is equivalent to (*myCardPtr).suit

Dale Roberts Structures Structure can be nested struct date { int day; int month; int year; int year_date; char month_name[4]; }; struct person { char name [NAME_LEN]; char address[ADDR_LEN}; long zipcode; long ss__number; double salary; struct date birthday; }; struct person emp; emp.birthday.month = 6; emp.birthday.year = 1776; Name Rule Members in different structure can have the same name, since they are at different position. struct s1 {.... char name[10];.... } d1; struct s2 {.... int name; } d2; struct s3 {.... int name; struct s2 t3;.... } d3; float name;

Dale Roberts Memory Layout Example: struct data1 { int day1; char month[9]; int year; }; Word (2 bytes) alignment machine – begins (aligns) at even address, such as PC, SUN workstation day1int2 bytes monthchar array9 bytes (hole)1 bytes yearint 2 bytes Quad (4 bytes) address alignment – begins (aligns) at quad address, such as VAX 8200 day1int4 bytes monthchar array9 bytes (hole)3 bytes yearint 4 bytes You must take care of hole, if you want to access data from very low level (i.e. low-level I/O, byte operations, etc.) integer 9 character (hole) integer 9 character (hole)

Dale Roberts sizeof Operator sizeof(struct tag) struct test { char name[5]; int i;/* assume int is 2 bytes */ char s; } t1, t2; main(){ printf(“sizeof(struct test) = %d\n”, sizeof (struct test)); printf(“address of t1 = %d\n”, &t1); printf(“address of t2 = %d\n”, &t2); printf(“address of t1.name = %d\n”, t1.name); printf(“address of t1.i = %d\n”, &t1.i); printf(“address of t1.s = %d\n”, &t1.s); }output: sizeof(struct test) = 10 address of t1 = 992 address of t2 = 1002 address of t1.name = 992 address of t1.i = 998 address of t1.s = bytes 5 bytes 1 byte (hole) 1 byte 1 byte (hole) bytes 5 bytes 1 byte (hole) 1 byte 1 byte (hole) t2 t1