Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pointers.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers and Classes.
Generic Programming in C
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Chapter 10-1: Structure.
Programming Languages and Paradigms
Data Structures and Algorithms
Programming Paradigms
Student Book An Introduction
Lecture 6 C++ Programming
Data Structures and Algorithms
Sorting.
14th September IIT Kanpur
C Arrays.
7 Arrays.
More About Data Types & Functions
Pointers and Pointer-Based Strings
Pointers.
Miscellaneous functions
Classes and Objects.
Review & Lab assignments
Data Structures and Algorithms
Qsort.
Linked Lists Chapter 4.
CS111 Computer Programming
Data Structures and Algorithms
Pointers Pointers point to memory locations
Passing Arguments and The Big 5
Java Programming Language
Chapter 9: Pointers and String
Data Structures and Algorithms
Module 14 Miscellaneous Topics
Programming Languages and Paradigms
Structures Structured Data types Data abstraction structs ---
EECE.2160 ECE Application Programming
Pointers.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Data Structures and Algorithms Functions as Data Types PLSD210

Quicksort - library implementation POSIX standard void qsort( void *base, size_t n, size_t size, int (*compar)( const void *, const void * ) ); base address of array n number of elements size size of an element compar comparison function

Quicksort - library implementation POSIX standard Comparison function C allows you to pass a function to another function! void qsort( void *base, size_t n, size_t size, int (*compar)( const void *, const void * ) ); base address of array n number of elements size size of an element compar comparison function

Functions as data types - declaration int (*compar)( const void *, const void * ); Parentheses around the * and the name Distinguish this as a pointer to a function Contrast with ... int *compar( const void *, const void * ); This is a function returning an int *

Functions as data types Declaration So this declares a pointer to a function named compar that takes two void * arguments and returns an int int (*compar)( const void *, const void * ); The function returns an int and has two arguments, both void *

Functions as data types Declaration The compare function passed to qsort returns -1 *arg1 < *arg2 0 *arg1 == *arg2 +1 *arg1 > *arg2 int (*compar)( const void *, const void * ); The function returns an int and has two arguments, both void * Interpret as defining order - not magnitude

Functions as data types Use Library functions that need a special purpose function Sorting needs a concept of order compar function provides it Ordering rule could be quite complex for a complex record eg names in a telephone directory Searching also needs an order Notes on Web and in C texts - omitted from printed copy

Functions as data types Generalising our collection class We used pointers to objects Enabling one collection class to store any type of object All C pointers are the same! but we had to assume external itemkey or itemcmp functions Restricting us to one collection per program! Add the ordering function to the attributes Ordering rule stored with each collection object As many collections as we like!

Generalised Collection Class Redefine the constructor Add an element to the attributes Collection ConsCollection( int max_items, int (*compar)(const void *,const void * ) ); struct t_collection { int max_items; int cnt; int (*compar)( const void *, const void * ); void *data; }; Assumes array - make appropriate changes for list, tree, etc.

Generalised Collection Class In the constructor code, treat the function just like any other data item: Collection ConsCollection( int max_items, int (*compar)(const void *,const void * ) ) { Collection c; c = malloc( sizeof( struct collection ) ); if ( c != NULL ) { c->max_items = max_items; c->compar = compar; …… } return c; compar is a pointer to a function - usually the address of the beginning of the function’s code Copy the pointer (it’s an address) just like any other data value!

Using a pointer to a function Use the attribute as the name of a function: void *FindInCollection( void *key ) { int k; for(k=0;k<c->cnt;k++) { if( *c->compar( key, c->data[k] ) == 0 ) return c->data[k]; } return NULL; … and supply it with arguments, just like any other function! c->compar is a pointer to a function - de-reference to access the function

Using a pointer to a function Use the attribute as the name of a function: void *FindInCollection( void *key ) { int k; for(k=0;k<c->cnt;k++) { if( *c->compar( key, c->data[k] ) == 0 ) return c->data[k]; } return NULL; … but the compiler will be kind and understand if you forget this asterisk! c->compar is a pointer to a function - de-reference to access the function

Functions as Data - Other uses Table-driven code eg menus Each entry String Action to be performed if the entry is selected struct menu { char *desc; int (*action)( void ); } file_menu[ ] = { { “Save”, save_function }, { “Save as”, save_as_function }, … }; Pointer to a function … which takes no arguments

Functions as Data - Other uses Table-driven code eg menus Each entry String Action to be performed if the entry is selected struct menu { char *desc; int (*action)( void ); } file_menu[ ] = { { “Save”, save_function }, { “Save as”, save_as_function }, … }; Functions which are called when the item is selected Strings which appear in the menu

Functions as Data - Other uses Using the table struct menu { char *desc; int (*action)( void ); } file_menu[ ] = { { “Save”, save_function }, { “Save as”, save_as_function }, … }; void file_menu_callback() { int k = item_selected( file_menu ); file_menu[k].action(); } Find which item was selected Call the action function