Structures.

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.
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 Programming Lecture 23 Enumeration Types Structures.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Structures Spring 2013Programming and Data Structure1.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
1 Review of Chapter 10: String and Pointers. 2 Outline  String:  Representation of a string: \0  Using scanf to read in string  Initilization of strings.
 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,
Structures and Lists (and maybe stacks) Chapters 9-10.
1 Review of Class on Nov 23:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
 Review structures  Program to demonstrate a structure containing a pointer.
13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };
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,
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Today’s Material Aggregate Data Types: Structures and Unions
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.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
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.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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.
Constructors and Destructors
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
CS1010 Discussion Group 11 Week 12 – Structured Structures.
Lecture 13 Structs.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
C Structures, Unions, and Enumerations
Learning Objectives Structures Structure types
Structures putting data together.
C Structures, Unions, Bit Manipulations and Enumerations
Structures and Union.
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Arrays and Arrays as Parameters
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Sudeshna Sarkar 7th March 2017
Classes and Objects.
Chapter 1: Introduction to Data Structures(8M)
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
By C. Shing ITEC Dept Radford University
Java Programming Language
Structures.
Structures and Union.
Structure (i.e. struct) An structure creates a user defined data type
Structures Structured Data types Data abstraction structs ---
CSCE 206 Lab Structured Programming in C
Structures Declarations CSCI 230
Presentation transcript:

Structures

Structures Array Structure A derived type used to represent homogeneous data Structure provides a means to aggregate variables of different types struct card { int pips; /* 1,2,3,…,13 */ char suit; /* ‘c’, ’d’, ‘h’, and ‘s’ */ }; a structure tag name This declaration creates the derived data type struct card. A user-defined type Just a template, no storage allocated

Structures struct card { int pips; char suit; }; struct card c1, c2; typedef struct card card; card c1, c2; Pips are small but easily countable items. The term is used to describe the dots on dominoes, dice, denote suits, and is the name for the small seeds of somefruits. It could be used as a synonym for dot in most situations, for example morse code.

Structures The member access operator . structure_variable.member_name c1.pips = 3; c1.suit = ‘s’; Structure assignment c1 = c2; struct card { int pips; char suit; } deck[52]; The identifier deck is declared to be an array of struct card.

Structures Within a given structure, member names must be unique. Members in different structures can have the same name. struct fruit { char *name; int calories; }; struct vegetable { struct fruit a; struct vegetable b; a.calories = 100; b.calories = 120;

Structures If a tag name is not supplied, then the structure type cannot be used in later declarations. struct { int day, month, year; char day_name[4]; /* Mon, Tue, Wed, etc. */ char month_name[4]; /* Jan, Feb, Mar, etc. */ } yesterday, today, tomorrow; vs. struct date { }; struct date yesterday, today, tomorrow;

Structures When using typedef to name a structure type, the tag name may be unimportant. typedef struct { float re; float im; } complex; complex a, b, c[100];

Accessing Members of a Structure [class_info.h] #define CLASS_SIZE 100 struct student{ char *last_name; int student_id; char grade; }; [grade.c] #include “class_info.h” int main() { struct student tmp, class[CLASS_SIZE]; tmp.grade = ‘A’; tmp.last_name = “Casanova”; tmp.student_id = 910017; … }

Accessing Members of a Structure /* Count the failing grades. */ #include “class_info.h” int fail(struct student class[]) { int i,cnt = 0; for (i=0; i<CLASS_SIZE; i++) cnt += class[i].grade == ‘F’; }  int fail(struct student *class)  cnt += (((class[i]).grade) == ‘F’);

Accessing Members of a Structure The member access operator -> access the structure members via a pointer pointer_to_structure->member_name  (*pointer_to_structure).member_name *pointer_to_structure.member_name  *(pointer_to_structure.member_name) ! ERROR

Accessing Members of a Structure [complex.h] struct complex{ double re; double im; }; typedef struct complex complex; [2_add.c] #include “complex.h” void add(complex *a, complex *b, complex *c) /* a = b+c */ { a->re = b->re + c->re; a->im = b->im + c->im; }

Accessing Members of a Structure

Operator Precedence and Associativity: A Final Look

Using Structures with Functions When a structure is passed as an argument to a function, it is passed by value A local copy is made for use in the body of the function. If a structure member is an array, the array gets copied as well. relatively inefficient !! struct dept { char dept_name[25]; int dept_no; } typedef struct { char name[25]; int employee_id; struct dept department; struct home_address *a_ptr; double salary; …. } employee_data;

Using Structures with Functions employee_data update(employee_data r) { …. printf(“Input the department number: “); scanf(“%d”, &n); r.department.dept_no = n; return e; } employee_data e; e = update(e); void update(employee_data *p) { …. printf(“Input the department number: “); scanf(“%d”, &n); p->department.dept_no = n; } employee_data e; update(&e);  (r.department).dept_no = n;  (p->department).dept_no = n;

Initialization of Structures card c = {13, ‘h’}; /* the king of hearts */ complex a[3][3] = { {{1.0, -0.1}, {2.0, 0.2}, {3.0, 0.3}}, {{4.0, -0.4}, {5.0, 0.5}, {6.0, 0.6}}, }; /* a[2][] is assigned zeros */ struct fruit frt = {“plum”, 150}; struct home_address { char *street; char *city_and_state; long zip_code; } address = {“87 West Street”, “Aspen, Colorado”, 80526}; struct home_address previous_address = {0};