13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
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.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
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.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
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,
Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
Structures and Lists (and maybe stacks) Chapters 9-10.
CS 201 Structure & Union Debzani Deb.
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.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.
 Review structures  Program to demonstrate a structure containing a pointer.
Functions, Pointers, Structures Keerthi Nelaturu.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
Summary. Data Types int A; I am declaring a variable A, which is an integer.
© 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.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
 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.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
10 주 강의 Bitwise operators and Enumeration types. 컴퓨터환경 8-bit bytes, 4-bytes words Two ’ s complement ASCII character codes.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Structures.
C Structures, Unions, Bit Manipulations and Enumerations
Structures and Union.
Derived types.
Structures and Union.
Chapter 10 Structures and Unions
C Structures, Unions, Bit Manipulations and Enumerations
By C. Shing ITEC Dept Radford University
Programming Language C Language.
Type compatibility and pointer operation
Structures and Union.
Structures.
Structures Declarations CSCI 230
Presentation transcript:

13 주 강의 Structures and Unions

Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };

Structures  struct card { int pips; char suit; }; struct cardc1, c2;  struct card { int pips; char suit; } c1, c2; ※ pips,suit 를 멤버로 가지는 card Type 의 c1,c2 선언

값 저장 c1.pips = 3; c1.suit = ‘ s ’ ; c2=c1; /* structure 전체를 복사 */ typedef struct card card; card c3, c4, c5;

다른 예 struct fruit { char *name; int calories; } struct vegetable { char *name; int calories; } struct fruit a; struct vegetable b;

예 struct card { int pips; char suit; } deck[52]; struct date { int day, month, year; char day_name[4]; /* Mon, Tue,.. */ char month_name[4]; /* Jan, … */ } yesterday, today, tomorrow;

Complex number typedef struct { float re; float im; } complex; complex a, b, c[100];

Accessing members #define CLASS_SIZE 100 struct student { char *last name; int student_id; char grade; }; struct student tmp, class[CLASS_SIZE]; tmp.grade = ‘ A ’ ; tmp.last_name = “ Casanova ” ; tmp.student_id = ; class[i].grade = ‘ F ’ ;

예제 설명 및 -> Page 412 프로그램 설명 -> – pointer_to_structure->member_name  (*pointer_to_structure).member_name /* () 가 없으면 오류 */ complex *a, *b, *c; – a->re = b-> re + c -> re;

Structures structure_declaration::=struct_specifier declarator_list; struct_specifier::=struct tag_name struct tag_name opt {{member_declaration} 1+ } tag_name::=identifier member_declaration::=type_specifier declarator_list; declarator_list::=declarator{,declarator} 0+

Member access operators Declarations and assignments struct student tmp, *p = &tmp; tmp.grade = ‘ A ’ ; tmp.last_name = “ Casanava ” ; tmp.student_id = ; ExpressionEquivalent expressionConceptual value tmp.gradep->gradeA tmp.last_namep->last_nameCasanava (*p).student_idp->student_id *p->last_name+1(*p->last_name))+1D *(p->last_name+2)(p->last_name)[2]s

Operator Precedence and Associativity OperatorAssociativity () []. -> ++(postfix) --(postfix)left to right ++(prefix) --(prefix) ! ~ sizeof(type) +(unary) -(unary) &(address) *(dereference) right to left * / %left to right + -left to right >left to right >=left to right == !=left to right & ^ | &&left to right ||left to right ?:right to left = += -= / = %= >>= <<= &= ^= |= right to left,(comma operator)left to right

Using structures with functions typedef struct { char name[25]; int employee_id; struct dept deparment; struct home_address *a_ptr; double salary; …. } employee_data;

Cont. struct dept { char dept_name[25]; int dept_no; }; e.department.dept_no  (e.department).dept_no employee_data e; e = update(e);

Initialization of Structures cardc={13, ’ h ’ }; struct fruitfrt={ “ 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};

Cont. complex[3][3] = { {{1.0, -0.1}, {2.0, 0.2}, {3.0,0.3}} {{4.0,-0.4}, {5.0,0.5}, {60.,0.6}} }; Poker game 설명

UNION 구조체와 같은 구문 형식이지만, 각 멤버 들은 같은 기억 장소를 공유 union int_ot_float { int i; float f; }; union int_or_float a,b,c;

예 union int_or_float n; n.i = 4444; n.f = 444.0;  n.i 4444  n.f e-41 n.f  n.i

다른 예 struct flower { char *name; enum{red, white, blue} color; }; struct fruit { char *name; int calories; }; struct vegetable { char *name; int calories; int cooking_time; }; struct flower_fruit_or_vegetable { struct flowerflw; struct fruitfrt; struct vegetableveg; }; union flower_fruit_or_vegetableffv; ffv.veg.cooking_time = 7;

p.425 예 /*In file numbers.x */ typedef union int_or_float { int i; float f; } number; int main() { number n; n.i = 4444; printf( “ i:%10d f:16.10e\n ”, n.i, n.f); n.f = ; printf( “ i:%10d f:16.10e\n ”, n.i, n.f); return 0; }

Bit Field struct pcard { unsigned pips : 4; unsigned suit : 2; } bit_field_number::={int|unsigned} 1 {identi fier} opt :expr expr ::=constant_integral_expression

Bit Field 의 예 struct abc { int a : 1, b : 16, c : 16; } x; struct small_integers { unsigned i1:7, i2:7, i3:7, :11, /*align to next word*/ i4:7, i5:7, i6:7; } struct abc { unsigned a:1, :0, b:1, :0, c:1; };

p.429 예 /* in file check_bits.x */ #include typedef struct { unsigned b0:8, b1:8, b2:8, b3:8; } word_bytes; typedef struct { unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b6:1, b7:1, b8:1, b9:1, b10:1,b11:1,b12:1, b13:1, b14:1, b15:1, b16:1, b17:1, b18:1, b19:1,b20:1, b21:1,b22:1, b23:1, b24:1, b25:1, b26:1, b27:1, b28:1,b29:1, b30:1,b31: } word_bits; typedef union { intI; word_bitsbit; word_bytesbyte; } word; void main() { word w = {0}; voidbit_print(int)l w.bit.b8 = 1; w.byte.b0 = ‘ a ’ ; printf( “ w.I = %d\n ”, w.I); bit_print(w.I); }

ADT Stack ADT(Abstract Data Type) Stack –last-in-first-out(LIFO) –push,pop,top,empty,full,reset 등 Stack 의 구현 (p.431) 문제 – 교재의 Stack 을 사용하여 아래의 식을 계산하라 ( ) * 8 = ??? 단, 위 식은 키보드에서 “ ( ) ” 를 포함한 수식을 입력받으며, 반드시 “ ( ) ” 에 대한 Error 처리가 되어야 한다. –( )) => ERROR!!! 사칙연산이 가능해야 한다