Systems Programming. 05. Structures & Trees. Alexander Holupirek

Similar documents
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; };

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS 241 Data Organization Binary Trees

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

Syntax and Variables

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

High Performance Computing

Types, Operators and Expressions

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

Types, Operators and Expressions

Programming for Engineers Structures, Unions

Lecture 02 C FUNDAMENTALS

A Fast Review of C Essentials Part I

CS Programming In C

C-LANGUAGE CURRICULAM

File Handling in C. EECS 2031 Fall October 27, 2014

Fast Introduction to Object Oriented Programming and C++

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

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

typedef int Array[10]; String name; Array ages;

Compiling and Running a C Program in Unix

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

Structures. Chapter 6

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

CS3157: Advanced Programming. Announcement

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

CS201 Some Important Definitions

Intermediate Programming, Spring 2017*

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

C: How to Program. Week /Mar/05

Scientific Programming in C X. More features & Fortran interface

CSCI 171 Chapter Outlines

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

OBJECT ORIENTED PROGRAMMING USING C++

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$

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

High Performance Computing in C and C++

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

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

INTRODUCTION 1 AND REVIEW

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

BLM2031 Structured Programming. Zeyneb KURT

Structures, Unions, and Typedefs

C Structures, Unions, Bit Manipulations, and Enumerations

Tokens, Expressions and Control Structures

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS1100 Introduction to Programming

Contents. Custom data type struct data type Fixed length memory Alias data type

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

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Read: Kernighan & Ritchie

Lecture 8: Structs & File I/O

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

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

Armide Documentation. Release Kyle Mayes

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

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

Functions BCA-105. Few Facts About Functions:

Chapter 2 - Introduction to C Programming

C for C++ Programmers

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Have examined process Creating program Have developed program Written in C Source code

CA341 - Comparative Programming Languages

LESSON 4. The DATA TYPE char

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

IV Unit Second Part STRUCTURES

QUIZ. What is wrong with this code that uses default arguments?

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Introduction to C++ Systems Programming

Introduction to string

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

C Programming Language (Chapter 2 of K&R) Variables and Constants

The New C Standard (Excerpted material)

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

Review of Important Topics in CS1600. Functions Arrays C-strings

Fundamental of Programming (C)

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions


Semi-structured Data: Programming. Introduction to Databases CompSci 316 Fall 2018

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Programming. Structures, enums and unions

Crit-bit Trees. Adam Langley (Version )

Part V. SAX Simple API for XML

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Part V. SAX Simple API for XML. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 84

Fundamental Concepts and Definitions

Contents. Preface. Introduction. Introduction to C Programming

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

Review of the C Programming Language

Approximately a Test II CPSC 206

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

Transcription:

Systems Programming 05. Structures & Trees Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008

Schedule for Today 2 Today: Finish introduction to the C programming language Structures, Unions, Enumerations, Typedefs Pointers to Function and Function Callbacks Putting it all together by using an external library

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Structures 4 A structure is a collection of one or more variables possibly of different types grouped together under a single name for convenient handling A structure organizes complicated data, particulary in large programs permits a group of related variables to be treated as a unit

Declare a structure: struct point { int x; int y; }; Structure Declaration (Example) 5 Some variables of that type: struct point here, there ; Combination of the upper two: struct point { int x; int y; } here, there ;

Structure Declaration 6 Keyword struct introduces a structure declaration: struct structure tag { /* list of member declarations */ type name; type name; } list of variable declarations; Structure declaration has four parts: keyword struct structure tag (optional) brace-enclosed list of declarations for the members (optional) list of variables of the new structure type (optional)

Struct Declaration Defines a Type 7 A struct declaration defines a type Terminating right brace may be followed by a list of variables: struct {... } x, y, z; This is syntactically analogous to: int x, y, z; Each statement declares x, y, and z to be variables of the named type and causes space to be set aside for them A structure declaration not followed by a list of variables reserves no storage merely describes a template or the shape of a structure

Structure Tag 8 Tagged structure A previous established structure tag can be used subsequently as a shorthand for the part of the declaration in braces: struct point pt; /* Structure tag as a shorthand */ Anonymous structure struct { int i; int j; } a;

Operations on and Initialization of Structures 9 Legal operations copying it assigning to it as a unit taking its address with & accessing its members Illegal operation Structures may not be compared Initialization A list of constant member values initializes a structure struct point maxpt = { 320, 200 }; An automatic structure may also be initialized by assignment by calling a function returning a struct of apt type

Structures and Functions Example 10 /* makepoint : make a point from x and y components */ struct point makepoint ( int x, int y) { struct point tmp ; } tmp.x = x; tmp.y = y; return tmp ; struct point p1; struct point p2; p1 = makepoint (0,0); p2 = makepoint ( XMAX, YMAX );

Structures and Functions 11 There is nothing special about structures and functions Pass/return components separately Pass/return entire structure Pass/return a pointer to structure If a large structure is to be passed to a function a pointer may be the better choice (pass by value copies the whole structure). Structure pointers are just like pointers to ordinary variables: struct point * pp; *pp = makepoint (1,3); /* *pp is the structure */ (* pp ).x += 2; /* (* pp ).x is a member */

The structure member operators. and -> 12 Structure operator. connects structure- and member name A member of a particular structure is referred to in an expression by structure-name.member printf ("%d,%d", pt.x, pt.y); Structure operator -> as shorthand If ps is a pointer to a structure with member m, than are equivalent by definition. (*ps).m ps->m

Nested Structures 13 Structures can be nested struct rect { struct point pt1 ; struct point pt2 ; } struct rect screen ; screen. pt1.x; The rect structure contains two point structures screen.pt1.x is the x coord. of the pt1 member of screen

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Self-referential Structures: Binary Tree 15 Data structure: Binary Tree (to store words lexicographically) One node per distinct word Each node contains: a pointer to the text of the word a count of the number of occurences a pointer to the left child node a pointer to the right child node This reads in C: struct tnode { char * word ; int count ; struct tnode * left ; struct tnode * right ; };

Lexicographic Order in Binary Tree 16 Each node has either zero, one or two children Given a node and its word Left subtree: All words are lexicographically less than word Right subtree: All words are lexicographically greater than word Consider the following input sentence: now is the time for all good men to come to the aid of their party

Output and Tree View of bintree.c 17 now is the time for all good men to come to the aid of their party 1 aid 1 all 1 come 1 for 1 good 1 is 1 men 1 now 1 of 1 party 2 the 1 their 1 time 2 to all for is good men now of party the their time to aid come

Source Code Binary Tree 18 # include <stdio.h> # include <ctype.h> # include < string.h> # define MAXWORD 100 struct tnode { char * word ; int count ; struct tnode * left ; struct tnode * right ; }; struct tnode * addtree ( struct tnode *, char *); void treeprint ( struct tnode *); int getword ( char *, int ); int main ( void ) { struct tnode * root ; char word [ MAXWORD ]; } root = NULL ; while ( getword (word, MAXWORD )!= EOF ) if ( isalpha ( word [0])) root = addtree (root, word ); treeprint ( root ); return (0); struct tnode * talloc (void ); char * strdupl ( char *); /* add a node with w, at or below p */ struct tnode * addtree ( struct tnode *p, char *w) { int cond ; if (p == NULL ) { /* new word arrived */ p = talloc (); /* make a new node */ p-> word = strdupl (w); p-> count = 1; p-> left = p-> right = NULL ; } else if (( cond = strcmp (w, p-> word )) == 0) p->count ++; /* repeated word */ else if ( cond < 0) /* less than -> left */ p->left = addtree (p->left, w); else p->right = addtree (p->right, w); return (p); }

Source Code Binary Tree 19 /* treeprint : in - order print of tree p */ void treeprint ( struct tnode *p) { if (p!= NULL ) { treeprint (p-> left ); printf ("%4d %s\n", p->count, p->word ); treeprint (p-> right ); } } # include < stdlib.h> /* talloc : make a tree node */ struct tnode * talloc ( void ) { struct tnode *tn; tn = ( struct tnode *) malloc ( sizeof ( struct tnode )); return tn; } /* make a duplicate of s */ char * strdupl ( char *s) { char *p; } p = (char *) malloc ( strlen (s) + 1); if (p!= NULL ) strcpy (p, s); return p; /* get single word from input */ int getword ( char *word, int max ) { int c, i; i = 0; while ((c = getchar ())!= EOF && i < max - 1 && c!= && c!= \n && c!= \t ) word [i ++] = c; word [i] = \0 ; return (c == EOF )? EOF : i; }

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Unions 21 A union is a variable that may hold (at different times) objects of different types and sizes. Unions provide a way to manipulate different kinds of data in a single area of storage. The syntax is based on structures: union u_tag { int ival ; float fval ; char * sval ; } u; The variable u will be large enough to hold the largest of the three types (the specific size is implementation-dependent) It is the programmer s responsibility to keep track of which type is currently stored in a union

Unions (cont.) 22 Unions may occur within structures and arrays, and vice versa Notation for accessing a member of a union in a structure (or vice versa) is identical to that for nested structures struct { char * name ; int flags ; int utype ; union { int ival ; float fval ; char * sval ; } u; } symtab [ NSYM ]; symtab [i].u. ival * symtab [ i]. u. sval /* first char of string sval */ symtab [i].u. sval [0]

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Enumerations 24 Enumerations provide a conventient way to associate constant values with names An alternative to #define with the advantage that the values can be generated A debugger may also be able to print values of enumeration variables in symbolic form enum boolean { NO, YES };

Enumerations (cont.) 25 An enumeration is a list of constant integer values First value in an enum has value 0, the next 1...... unless explicit values are specified enum escapes { BELL = \ a, BACKSPACE = \b, TAB = \t }; If not all values are specified, unspecified values continue the progression from the last specified value /* FEB is 2, MAR is 3... */ enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Typedef: New Data Type Names 27 typedef is a facility for creating new data type names: typedef int Length ; makes the name Length a synonym for int Type Length can be used exactly in the same way as type int Reasons for using typedefs Portability issues Types like size t, ptrdiff t are examples (Better) Documentation for a program A type Treeptr may be easier to understand than one declared as a pointer to a complicated structure

Further typedefs 28 In effect, typedef is like #define Except that it is interpreted by the compiler Therefore its capabilities are beyond textual substitutions typedef int (* PFI )( char *, char *); Creates the type PFI (pointer to function (of two char * argument) returning int typedef enum { ON, OFF, BROKEN } state ; Creates the type state with three possible values

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Pointers to Functions 30 A function itself is not a variable But it is possible to define pointers to functions which can be assigned, placed in arrays passed to functions, returned by functions int (* func )( char *, char *); Declares a pointer to a function that has two char * arguments and returns an int func is a pointer to a function (*func) is the function, as such the function call reads: (* comp )("abc ", " def ");

# include < stdio.h> void print_one ( void ) { printf ("1\n"); } void print_two ( void ) { printf ("2\n"); } Pointer to Functions Example 31 int main ( void ) { void (* func [])( void ) = { print_one, print_two }; } (* func [0])(); (* func [1])(); return (0);

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

Dealing with XML trees using SAX 33 Putting it all together Establish function callbacks in an event driven application Use structures with function pointers to register for callbacks Further deal with trees, more precise XML trees Use an external library able to parse trees We will build a SAX parser application for XML documents

An API to Handle XML Trees 34 SAX (Simple API for XML) SAX is a quasi-standard for parsing XML documents. We will use the C library libxml2 1. SAX processors operate in a streaming fashion and with constant space, regardless of the document size. The SAX parser reads its input sequentially, and once only. 1 Originally developed for the GNOME project

Tree View of an XML Document 35 <a> <b> <c> <d/> <e/> </c> </b> <f> <g/> <h> <i/> <j/> </h> </f> </a> d b c e a g f i h j An XML document and its inner tree structure.

Parsing Process Triggers Events 36 SAX Parser reports. During the parsing process the parser reports interesting events to registered applications, such as: The occurence of a start or end tag, a piece of simple text, or the beginning/end of the document. Registered application reacts. The application implements code to react in parallel (to the parsing process) to fired events. Syntactical errors in the XML document will be detected by the parser and reported to the application.

SAX Events 37 Selected events defined by the SAX interface 2 : Event... triggered by Formal arguments startdocument <?xml...?> enddocument EOF startelement <t a 1 = v 1... a n = v n > t, (a 1, v 1 ),..., (a n, v n ) endelement </t> t characters text content buffer pointer, length comment <!-- c --> c.. Be careful with the characters event! For performance reasons the parser will give you a pointer to its own memory space. Never write to this memory, and never look further than the length given by the parser!. 2 Complete documentation http://www.saxproject.org/

SAX Events Example 38 <?xml version ="1.0" encoding="iso -8859-1"? > <fs > <dir name =" etc "> <file name =" services "> # $OpenBSD : services,v 1.67 2007/05/01 11:48:40 steven Exp $ # # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single... </file > </dir > <dir name =" usr "/> </fs > Event startdocument startelement startelement startelement characters endelement endelement. Actual arguments t = "fs" t = "dir", a 1 = "name", v 1 = "etc" t = "file", a 1 = "name", v 1 = "services" c = "# $OpenBSD: services...", len = n t = "file" t = "dir".

Function Callbacks 39 Events are reported to the application via callback functions The application has to register them before parsing. Populate a callback function table. Hand over the callback function table to the parser. Invoke the parsing process. Whenever any of the SAX event occurs, the parser calls the function that is registered for this event.

Basics of Structures Self-referential Structures Unions Enumerations Typedef Pointers to Functions Function Callbacks The libxml2 library

The libxml2 Library 41 Correct function type is mandatory to interface with libxml2: void sax_start_document ( void * ctx ); void sax_end_document ( void * ctx ); void sax_start_element (void *ctx, const xmlchar *t, const xmlchar ** atts ); void sax_end_element ( void *ctx, const xmlchar *t); void sax_characters ( void *ctx, const xmlchar *c, int len ); The *ctx ( context ) pointer stores private application data. Its value can be set before parsing. The same pointer is passed through with every callback.

Callback C Code Example 42 A simple character callback function definition: void sax_characters ( void * ctx, const xmlchar *c, int len ) { int i; for ( i = 0; i < len ; i ++) printf ("%c", c[i ]); } The corresponding typedef in libxml/parser.h: /** * characterssaxfunc : * @ctx : the user data ( XML parser context ) * @ch : a xmlchar string * @len : the number of xmlchar * * Receive some chars from the parser. */ typedef void (* characterssaxfunc ) ( void * ctx, const xmlchar * ch, int len );

Callback Function Table 43 Our callback functions need to be registered...... in the callback function table of the parser. libxml2 declares a structure called xmlsaxhandler: struct xmlsaxhandler { startdocumentsaxfunc startdocument ; enddocumentsaxfunc enddocument ; startelementsaxfunc startelement ; endelementsaxfunc endelement ; characterssaxfunc characters ;... };

Populate a callback function table 44 # include <libxml / SAX.h> # include < libxml / parserinternals.h > /* define a callback function table */ xmlsaxhandler sax_ handler ; /* function to be called back ( characters event ) */ static void sax_ characters ( void * ctx, const xmlchar *c, int len ) {... } int main ( void ) { /* register callback function */ sax_ handler. characters = sax_ characters ;

Pass Callback Table and Parse 45 /* context pointer */ xmlparserctxtptr ctx ; /* create new parser instance */ ctx = xmlcreatefileparserctxt (" fs. xml "); /* pass callback table */ ctx -> sax = & sax_handler ; /* start parsing */ xmlparsedocument ( ctx ); } return (0); Instantiate a new parser. Hand over the callback function table to the parser. Invoke the parsing process.

Source Code Parser Application Example 46 1 # include <stdio.h> 2 # include < libxml /SAX.h> 3 # include < libxml / parserinternals.h> 4 5 static xmlsaxhandler sax_handler ; 6 7 /* characters callback function 8 * invoked for each text content */ 9 static void 10 sax_characters ( void *ctx, 11 const xmlchar *c, int len ) 12 { 13 int i; 14 15 for (i = 0; i < len ; i ++) 16 printf ("%c", c[i ]); 17 } Course repository: pub/src/sax xmp.c 19 int 20 main ( void ) 21 { 22 /* context pointer */ 23 xmlparserctxtptr ctx ; 24 25 /* register callback function */ 26 sax_handler. characters = sax_characters ; 27 28 /* create new parser instance */ 29 ctx = xmlcreatefileparserctxt ("fs.xml "); 30 if ( ctx == NULL ) { 31 printf (" error reading file "); 32 return ( -1); 33 } 34 35 /* pass callback table */ 36 ctx ->sax = & sax_handler ; 37 38 /* start parsing */ 39 xmlparsedocument ( ctx ); 40 41 return (0); 42 }

Compiling Applications with libxml2 47 Compilation Phase cc - Wall enable a lot of warnings -I/ usr / local / include / libxml2 the libxml2 header files -I/ usr / local / include character set conversion -c do not link yet, compile only sax_xmp. c the C file we want to compile Linking Phase cc - Wall enable a lot of warnings -L/ usr / local / lib location of shared object - lxml2 include the libxml2 code sax_xmp. o object files to link together

Compilation Session CIP Pool 48 Two step compilation in CIP Pool (Linux) mond10 :~ > cc -Wall -I/ usr / include / libxml2 -c sax_xmp.c mond10 :~ > cc - Wall - lxml2 sax_xmp. o At one sweep mond10 :~ > cc -Wall -I/ usr / include / libxml2 -lxml2 sax_xmp.c