Tin học cơ sở 4$ Structures!

Similar documents
CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Unit IV & V Previous Papers 1 mark Answers

3.3 Structures. Department of CSE

A Fast Review of C Essentials Part I

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

File Access. FILE * fopen(const char *name, const char * mode);

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Fundamentals of Programming

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

SAE1A Programming in C. Unit : I - V

Self-referential Structures and Linked List. Programming and Data Structure 1

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

IV Unit Second Part STRUCTURES

OBJECT ORIENTED PROGRAMMING

CSCI 171 Chapter Outlines

Structures. 21 July 2009 Programming and Data Structure 1

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lesson #8. Structures Linked Lists Command Line Arguments

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

[0569] p 0318 garbage

Midterm CSE 131 Winter 2012

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CS 61c: Great Ideas in Computer Architecture

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Pointers, Dynamic Data, and Reference Types

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Standard File Pointers

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Kurt Schmidt. October 30, 2018

We do not teach programming

Bit Manipulation in C

return return else return

struct Definition Structure Declaration (7.1) Chapter 7 - Introduction to Classes and Objects

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

S-1. Concepts this lecture. CSE 142 Computer Programming I. Chapter 11. Review: Data Structures. Problem: Account Records.

Lab # 4. Files & Queues in C

Guide for The C Programming Language Chapter 4

Tema 6: Dynamic memory

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Input / Output Functions

2009 S2 COMP File Operations

EM108 Software Development for Engineers

Stream Model of I/O. Basic I/O in C

Advanced C Programming and Introduction to Data Structures

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

AIMS Embedded Systems Programming MT 2017

25.2 Opening and Closing a File

BLM2031 Structured Programming. Zeyneb KURT

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Fundamental of Programming (C)

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Computer Systems Principles. C Pointers

C for C++ Programmers

CS 314 Principles of Programming Languages. Lecture 11

EL2310 Scientific Programming

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Character Strings. String-copy Example

Summer June 15, 2010

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

Chapter 10 C Structures, Unions, Bit Manipulations

File IO and command line input CSE 2451

Variation of Pointers

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Chapter-14 STRUCTURES

C programming basics T3-1 -

File I/O. Last updated 10/30/18

Structures. Lecture 15 COP 3014 Spring April 16, 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Storage class and Scope:

Week 3 Lecture 2. Types Constants and Variables

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Arrays. strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */

C: How to Program. Week /Mar/05

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

F4104 ALGORITHM & DATA STRUCTURE

Pointers as Arguments

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Arithmetic Expressions in C

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

What have we learned about when we learned about function parameters? 1-1

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Computer Programming Unit v

Pointers and File Handling

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Transcription:

Tin học cơ sở 4$ Structures!

Outline$ Structured Data types! Type definitions! Self-referential types: linked list!

Structure$ Many structured collections of data are:! Heterogeneous (components are different types)! Fixed-size (well-defined set of components)! For example:!! Student Record$ Name! Nguyen Van A! Student Id! 1234567! Course! CSE! Date of Birth! 01/01/1990! Gender! Male!

Structured data types$ A structure is a collection of variables, perhaps of different types, grouped together under a single name.! Structures:! Help to organize complicated data into manageable entities.! Expose the connection between data within an entity! Are defined using the struct keyword.!

Structs$ In C, struct models such data.! To define a record type, we must give:! Name of struct! Name of each field! Type of each field (could be another record/struct)! Example:! struct employee { char name[30]; int id; char position[20]; float salary;" };" struct employee e1, e2;"

Structs$ A record variable can be used as follows:! Assigned to another variable of the same type! Passed as a parameter to a function! To select a particular field (component) using the dot operator. Fields of a record behave exactly like variables of the appropriate type.! struct student john, betty;" struct student comp1721_students[200];" strcpy(john.name, "John Smith");" john.student_number = 2116344;" betty = john;" strcpy(betty.name, "Betty Smith");" betty.student_number = 2116345;" comp1721_students[0] = john;" comp1721_students[1] = betty;"!

Structs$ Operations which are NOT defined for whole records:! compare for equality/inequality!!(i.e. john == betty is not a legal expression)! compare based on ordering (<, >,...)!!(i.e. john < betty is not a legal expression)! arithmetic operations!!(i.e. john + betty is not a legal expression)! reading from or writing to text files!!(i.e. no cout << john; or equivalent)! If you need such operations, write your own procedures and functions.! Unlike arrays, it is possible to copy all components of a structure in a single assignment.!

Structs and Pointers$ As for other types:! (struct x *) is a pointer to struct x! & gives the address of a structure! There are precedence problems because. binds more tightly than *! If p is a pointer to a struct and a is a field of the struct:! *p.a means *(p.a) which is illegal.! You must use (*p).a! For convenience, the operator -> combines indirection and component selection.! p->a is equivalent to (*p).a!

Structs and Pointers$ For example! struct point { " };" "int x, y;" struct point a;" struct point *ap;" ap = &a;" *ap.x = 0; /* wrong - equivalent to *(ap.x) = 0;*/" (*ap).y = 0; /* right */" ap->x = 0; /* right */"

Type Definition$ We can use the keyword typedef to make our own definitions:! typedef float Floating;" This means variables can be declared as Floating but they will actually be of type float.! If we later decide we need more precision, we can change to:! typedef double Floating;! without changing the codes.!

Combining struct and typedef$ typedef struct employee Employee;" struct employee { char name[30]; int id; char position[20]; float salary;" };" Employee john;! Note: we use the convention that the name of the defined type is the same as the struct modifier, but with the first letter capitalized.!

Passing Structures as parameters$ A structure can be passed as a parameter to a function:! void print_employee (Employee e){! printf( Name: %s\n, e.name);! }! Because parameters in C are call-by-value, a copy of the entire structure will be made, and only this copy will be passed to the function.! If a function needs to modify components within the structure, or if we want to avoid the inefficiency of copying the entire structure, we can instead pass a pointer to the structure as a parameter (Employee *e).!

Self-referential types$ A very powerful programming technique is to create struct with fields which contain a reference to an object in the same struct. For example:! struct list_node { " "int data; " "struct list_node *next;" };" This approach can be used to create some very useful data structures.!

Linked Lists$ Consider the following data structure:! A list that grow over time! Items are added one by one! Each item is a number! It might grow like this:!

Linked Lists$ How do you implement such a list in C?! You can use an array but you must check the array doesn't overflow and allocate memory for a new larger array if the array fills.! Also can be implemented using a self-referential type.! Each element in the linked list need:! some information (the data)! a connection to the next item! The individual elements in data structures like this are often called nodes.!

Linked Lists$ A node could be represented by this! struct list_node { " "int data; " }" "struct list_node *next;" How can we represent the whole list?! We need a pointer to the first node:!!struct list_node *list_start;" How can we represent the ``end of list''?!!the next field of the struct will be NULL.!

Linked Lists$

Some methods:! insert a node! append a node! delete a node! print a list! Linked Lists$

File Handling$ stdin: keyboard, stdout: screen! FILE in <stdio.h>! FILE *fp;! FILE *fopen(char *name, char* mode);! Mode: r: read, w:write, a:append, b: binary! int fscanf(file *fp, char *format,.);! int fprintf(file *fp, char *format,.);! All files must be closed: fclose(fp);!!!

References$ The C Programming Language. Chapter 6.!