COMP2521. Generic ADTs in C

Similar documents
Array Initialization

Abstract Data Types. Lecture 05 Summary. Abstract Data Types Structures in C 1/26/2009. Slides by Mark Hancock (adapted from notes by Craig Schock)

CS 1713 Introduction to Programming II

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Programming in C. Pointers and Arrays

Pointers. Pointer References

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

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

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

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

Today s Learning Objectives

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Understanding Pointers

Pointers. Pointers. Pointers (cont) CS 217

CS 1713 Introduction to Programming II

CA341 - Comparative Programming Languages

Lecture 04 Introduction to pointers

CSC 2400: Computer Systems. Arrays and Strings in C

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

COMP 2355 Introduction to Systems Programming

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

CSC 2400: Computer Systems. Arrays and Strings in C

CS 261 Data Structures. Introduction to C Programming

Coursework 2: Basic Programming

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

ECE 15B COMPUTER ORGANIZATION

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

COMP1917: 09 Arrays and Strings

C programming basics T3-1 -

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Common Misunderstandings from Exam 1 Material

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lesson #8. Structures Linked Lists Command Line Arguments

CS 0449 Sample Midterm

CS 223: Data Structures and Programming Techniques. Exam 2

Arrays and Pointers (part 2) Be extra careful with pointers!

C programming for beginners

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Fast Introduction to Object Oriented Programming and C++

Kurt Schmidt. October 30, 2018

Computer Systems Assignment 2: Fork and Threads Package

Arrays and Pointers in C. Alan L. Cox

CS 3305 Intro to Threads. Lecture 6

8. Characters, Strings and Files

C Programming for Engineers Functions

COMP 2355 Introduction to Systems Programming

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

PROGRAMMAZIONE I A.A. 2017/2018

Project 1: How to Make One Dollar

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

CMPE-013/L. Introduction to C Programming

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Pointers and scanf() Steven R. Bagley

Lecture Notes on Generic Data Structures

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Personal SE. Functions, Arrays, Strings & Command Line Arguments

York University Faculty Science and Engineering Fall 2008

CSE 333 Lecture 7 - final C details

Functions & Memory Maps Review C Programming Language

Binghamton University. CS-211 Fall Variable Scope

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lesson 6.1: Structs. This declares a collection of two integer variables to denote the two coordinates of a point in a plane.

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.


unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

C++ Important Questions with Answers

CS113: Lecture 4. Topics: Functions. Function Activation Records

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

CS61, Fall 2012 Section 2 Notes

Lecture 03 Bits, Bytes and Data Types

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

Programming in C UVic SEng 265

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

Strings. Compare these program fragments:

Supporting Class / C++ Lecture Notes

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

return return else return

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

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

CS201 - Lecture 1 The C Programming Language

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

Topics Introduction to Microprocessors

ch = argv[i][++j]; /* why does ++j but j++ does not? */

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Exercise Session 2 Simon Gerber

BSM540 Basics of C Language

Arrays and Pointers (part 2) Be extra careful with pointers!

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

C PROGRAMMING Lecture 5. 1st semester

Transcription:

COMP2521 Generic ADTs in C 1

Function Pointers C can pass functions by passing a pointer to them. Function pointers... are references to memory addresses of functions are pointer values and can be assigned/passed Function pointer variables/parameters are declared as: typeofreturnvalue (*fp)(typeofarguments) In the following example, fp points to a function that returns int and have one argument of type int. int (*fp)(int) COMP2521 2

Function Pointers int square(int x) { return x*x; int timestwo(int x) {return x*2; int (*fp)(int); fp = □ //fp points to the square function int n = (*fp)(10); //call the square function with input 10 fp = timestwo; //works without the & //fp points to the timestwo function n = (*fp)(2); //call the timestwo function with input 2 n = fp(2); //can also use normal function call //notation COMP2521 3

Higher-order Functions Functions that get other functions as arguments, or return functions as a result Example: the function traverse takes a list and a function pointer (fp) as argument and applies the function to all nodes in the list void traverse (list ls, void (*fp) (list) ){ list curr = ls; while(curr!= NULL){ // call function for the node fp(curr); curr = curr->next; Second argument is fp, Pointer to a function like, void functionname(list n) COMP2521 4

Higher-order Functions: Example void printnode(list n){ if(n!= NULL){ printf("%d->",n->data); void traverse (list ls, void (*fp) (list)); void printgrade(list n){ if(n!= NULL){ if(n->data >= 50){ printf( Pass ); else { printf( Fail ); //The second argument must have matching prototype traverse(mylist, traverse(mylist, printnode); printgrade); COMP2521 5

Generic Types in C Polymorphism: refers to the ability of the same code to perform the same action on different types of data. There are two primary types of polymorphism: Parametric polymorphism: The code takes the type as a parameter, either explicitly (as C++ and Java) or implicitly (say as in C) Subtype polymorphism: Subtype polymorphism is associated with inheritance hierarchies. COMP1927 6 Lectures slides on the topic Generic Types in C are drawn from the material available at http://web.eecs.utk.edu/~bvz/cs365/notes/generic-types.html, by Brad Vander Zanden

Generic Types in C Polymorphism in C: C provides pointer to void (for example, void *p), the programmer can create generic data types by declaring values to be of type "void *". For example: struct Node { void *value; struct Node *next; ; The programmer can pass in type-specific functions (e.g., comparator functions) that take void *'s as parameters and that downcast the void *'s to the appropriate type before manipulating the data. For example, the example on the next page has a generic min function that computes and returns the minimum of two elements. The sample program compares two strings. COMP2521 7

#include <stdio.h> #include <string.h> COMP1927: Generic Types in C // generic min function void *min(void *element1, void *element2, int (*compare)(void *, void *)) { if (compare(element1, element2) < 0) return element1; else return element2; // stringcompare downcasts its void * arguments to char * and then passes // them to strcmp for comparison int stringcompare(void *item1, void *item2) { return strcmp((char *)item1, (char *)item2); int main(int argc, char *argv[]) { if (argc!= 3) { printf("usage: min string1 string2\n"); return 1; // call min to compare the two string arguments and downcast the return // value to a char * char *minstring = (char *)min(argv[1], argv[2], stringcompare); printf("min = %s\n", minstring); Lectures return slides 0; on the topic Generic Types in C are drawn from the material available at http://web.eecs.utk.edu/~bvz/cs365/notes/generic-types.html, by Brad Vander Zanden COMP1927 8

Advantages Generic Types in C One copy of the code works with multiple objects. The approach supports both generic data structures and generic algorithms. Disadvantages Downcasting can be dangerous, since run-time type checks are not performed in C. The code often has a cluttered appearance. COMP1927 9 Lectures slides on the topic Generic Types in C are drawn from the material available at http://web.eecs.utk.edu/~bvz/cs365/notes/generic-types.html, by Brad Vander Zanden

Generic Set ADT Live Demo of... Generic Set ADT Implementation COMP2521 10