Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Similar documents
Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

INTRODUCTION 1 AND REVIEW

A Fast Review of C Essentials Part I

Syntax and Variables

C Programming Review CSC 4320/6320

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

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

C Language, Token, Keywords, Constant, variable

IV Unit Second Part STRUCTURES

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

12 CREATING NEW TYPES

BLM2031 Structured Programming. Zeyneb KURT

Programming. Structures, enums and unions

C: How to Program. Week /Mar/05

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Computer Systems Principles. C Pointers

CMPE-013/L. Introduction to C Programming

Introduction to Programming (Java) 4/12

Programming in C++ 4. The lexical basis of C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 2 - Introduction to C Programming

COMP 2355 Introduction to Systems Programming

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

6.096 Introduction to C++ January (IAP) 2009


LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Short Notes of CS201

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Programming in C - Part 2

CS201 - Introduction to Programming Glossary By

Intermediate Programming, Spring 2017*

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Data Types and Variables in C language

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Programming and Data Structures

Tokens, Expressions and Control Structures

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

C PROGRAMMING Lecture 4. 1st semester

Computer System and programming in C

Creating a C++ Program

Programming languages - C

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

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

ET156 Introduction to C Programming

C Syntax Out: 15 September, 1995

Programming. Elementary Concepts

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

Programming for Engineers Structures, Unions

ET156 Introduction to C Programming

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSCI 171 Chapter Outlines

High Performance Computing in C and C++

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

LEXICAL 2 CONVENTIONS

Review of the C Programming Language for Principles of Operating Systems

Chapter 2: Overview of C. Problem Solving & Program Design in C

Fundamental of Programming (C)

The New C Standard (Excerpted material)

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

Lecture 8: Structs & File I/O

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

Motivation was to facilitate development of systems software, especially OS development.

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

CSE 230 Intermediate Programming in C and C++

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

UNIT- 3 Introduction to C++

Lecture 2: C Programming Basic

The New C Standard (Excerpted material)

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

ANSI C Programming Simple Programs

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

Motivation was to facilitate development of systems software, especially OS development.

Review of the C Programming Language

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

DEPARTMENT OF MATHS, MJ COLLEGE

COMPUTER APPLICATION

Programming. C++ Basics

UNIT - V STRUCTURES AND UNIONS

EC 413 Computer Organization

OBJECTIVE QUESTIONS: Choose the correct alternative:

Input And Output of C++

C Language Programming

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Introduction to C++ with content from

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Transcription:

Structures Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava 1/18

Derived and Structured Data Types basic data type - part of the standard language, preprocessor - without parameters, usees macros rather choose constants derived data types typedef <type definition> <identifier>; own definition of the data type of structured data types struct and union and their own structure definition. 2/18

Derived and structred data types declaration typ name; typ name[]; typ name[3]; typ *name; typ *name[]; typ *(name[]); typ (*name)[]; typ name(); typ *name(); typ *(name()); typ (*name)(); importance typ, array of typ, array (fixed size) of three items of type typ (name[0], name[1], name[2]), pointer of typ, (open) array of pointers to type typ, (open) array of pointers to type typ, a pointer to the (open) type field type typ, function returning a value of type typ, function returns a pointer to a value of type typ, function returns a pointer to a value of type typ, pointer to function returning typ. 3/18

General procedure - from the inside out. The procedure for the correct interpretation of the definition: 1 start with the right ID and look for round or square brackets (if any); 2 interpret these two brackets on the left and look for the asterisk; 3 if we encounter a right parenthesis (at any level of nesting), let us return and apply the rules one and two for everything between the parentheses; 4 apply the type specification. 4/18

Example of a more complex type design char *( *( *var) ()) [10]; 7 6 4 2 1 3 5 1 identifier is declared as var 2 pointer 3 function, which returns 4 pointer 5 array of ten elements, which are 6 pointers to 7 values of type char. 5/18

Example of a more complex type design 2 unsigned int *(*const *name[5][10]) (void); The identifier name is a two-dimensional array by a total of fifty elements. The elements of this array are pointers to the parameters that are constant. These constant indicators show the type of function that has pointer arguments and returns a value of type unsigned int. double (*var (double (*)[3])) [3] The function returns a pointer to an array of three values of type double. The argument, as well as the return value is a pointer to an array of three elements of type double. The argument of the function is a construct called abstract declaration. Generally it is a declaration without an identifier. To simplify and clarify the use of abstract declarations use typedef structure. 6/18

declaration int * int *[3] int (*)[5] int *() int (*) (void) int (*const []) (unsigned int,...) importance pointer to type int, array of three pointers to int, pointer to an array of five elements of type int fce without specifying arg. returning uk. int, uk. the destitute fci arguments returning int, uk. for an unspecified number of constant passed to the function each of which has a first argument in unsigned int and an unspecified number of additional arguments. 7/18

Allows you to define an enum constants. enum [tag] enum-list [declarator]; 1 enum is a keyword initiating definition of values enumerated type; 2 tag is optional tag, used mainly in the style of K & R C, with a structure typedef frequency of its use is declining; 3 enum-list is a list of constants enumerated type with possible explicitly assigned value, see example below, otherwise it becomes the first enum constant value of zero, the second value one,... Each successor has a value one greater than its predecessor; 4 declarator is optional list of variables of type enum. 8/18

enumerator typedef enum { Back = 8, Tab = 9, Esc = 27, Enter = 13, Down = 0 x0150, L e f t = 0 x014b, R i g h t = 0 x014d, Up = 0 x0148, NUL = 0 x0103, S h i f t Tab = 0x010f, Del = 0x0153, End = 0x014f, Home = 0x0147, Ins = 0x0152, PgDn = 0x0151, PgUp = 0x0149 } key t; int key;... else if ((key == Left) (key == Back))... else if (key == Enter)... else if (key == Esc)... else if... 9/18

Structs struct [< struct type name>] { [< type><variable name [, v a r i a b l e name,... ] > ] ; [< type><variable name [, v a r i a b l e name,... ] > ] ;... } [< structure variables >] ;. dot operator - selector of the memebre of the struct (static) -> - selector of struct members using pointer declarations 10 / 18

Struct example typedef struct { float re, im;} complex ; complex cislo, im jednotka = {0, 1} ; cislo.re = 12.3456; cislo.im = 987.654; printf( re = %10.5f im = %10.5f\n, im jednotka. re, im jednotka.im); printf( re = %10.5f im = %10.5f\n, cislo.re, cislo.im); 11 / 18

typedef struct { int ev cislo ; char nazev [ZNAKU NAZEV + 1 ] ; int na sklade ; float cena; } vyrobek ; typedef vyrobek zbozi [POLOZEK ZBOZI ] ; vyrobek a = {8765, nazev zbozi na sklade, 100, 123.99} ; vyrobek ppolozky ;... ppolozky >ev cislo = 1; 12 / 18

polozky [0]. ev cislo = 0; strcpy(polozky [0]. nazev, polozka cislo 0 ); polozky [0]. na sklade = 20; polozky [0]. cena = 45.15; ppolozky = polozky + 1; ppolozky >ev cislo = 1; / ( ppolozky ). ev cislo = 1; / strcpy(ppolozky >nazev, polozka cislo 1 ); ppolozky >na sklade = 123; ppolozky >cena = 9945.15; printf(formatvyrobek, a. e v cislo, a. na sklade, a.cena, a. nazev ); printf(formatvyrobek, p o l o z k y [ 0 ]. e v cislo, polozky [0]. na sklade, polozky [0]. cena, polozky [0]. nazev ); printf(formatvyrobek, p p o l o z k y >ev cislo, ppolozky >na sklade, ppolozky >cena, ppolozky >nazev ); 13 / 18

re = 0.00000 im = 1.00000 re = 12.34560 im = 987.65399 cislo : 8765 pocet: 100 cena: 123.99 nazev:nazev zbozi na sklade cislo : 0 pocet: 20 cena: 45.15 nazev:polozka cislo 0 cislo : 1 pocet: 123 cena: 9945.15 nazev:polozka cislo 1 14 / 18

Structure of FILE Type FILE, defined in stdio.h header file is: typedef struct { short level ; unsigned flags ; char fd ; unsigned char hold ; short bsize ; unsigned char buffer, curp ; unsigned istemp ; short token ; } FILE ; 15 / 18

Incomplete structures declaration struct A; / neuplna / struct B { struct A pa } ; struct A { struct B pb } ; the solution is made possible by the pointer whose its size is known. 16 / 18

union [< union type name>] { <type><variable names> ;... } [< union variables >] ; Syntax as a struct. Semantics (!) - Union of the items can be used at any one time only once. Implementation: memory space, reserved for the union is so large to accommodate a single (largest memory item). It is up to the programmer who is working with the union element to ensure that it is designed properly. 17 / 18

Bit Field Bit field is an integer, placed in the specified number of bits. These bits form a contiguous area of memory. Bit field can contain multiple integer entries. We can create a bit array of three classes: 1 free bit field 2 signed bit field, 3 unsigned bit field. Bit fields can be declared only as members of the struct or union. An expression that we write for an item identifier and the colon represents the field size in bits. 18 / 18

Struktura ftime detailně. struct ftime { unsigned ft tsec : 5; / Two s e c o n d s / unsigned ft min : 6; / Minutes / unsigned ft hour : 5; / Hours / unsigned ft day : 5; / Days / unsigned ft month : 4; / Months / unsigned ft year : 7; / Year 1980 / } ; Petr Šaloun (katedra informatiky FEI VŠB-TU Ostrava) Odvozené a strukturované typy dat 14. listopadu 2011 19 / 20