Generic Swap. void swap (void *ap, void *bp, int size) { char temp[size]; memcpy (temp, ap, size); memcpy (ap, bp, size); memcpy (bp, temp, size); }

Similar documents
Structures and Pointers

Character Strings. String-copy Example

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays. Steven R. Bagley

Intermediate Programming, Spring 2017*

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

Function Pointers. Function Pointers

Kurt Schmidt. October 30, 2018

Pointers, Dynamic Data, and Reference Types

Intermediate Programming, Spring 2017*

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Applications of Pointers (1A) Young Won Lim 12/26/17

Instructor (Jerry Cain)

1d: tests knowing about bitwise fields and union/struct differences.

C programming basics T3-1 -

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS 222: Pointers and Manual Memory Management

C Pointers. 6th April 2017 Giulio Picierro

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

Arrays, Pointers and Memory Management

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Language comparison. C has pointers. Java has references. C++ has pointers and references

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers (1A) Young Won Lim 11/1/17

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Memory, Arrays & Pointers

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Understanding Pointers

[0569] p 0318 garbage

Dynamic Memory Allocation

primitive arrays v. vectors (1)

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Advanced Systems Programming

REFERENCES, POINTERS AND STRUCTS

Algorithms & Data Structures

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Midterm Review. CS 211 Fall 2018

C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Arrays and Pointers (part 1)

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CA31-1K DIS. Pointers. TA: You Lu

ESc101: Pointers and Arrays

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

C++ Important Questions with Answers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

ECE551 Midterm Version 1

Principles of C and Memory Management

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CS107, Lecture 9 C Generics Function Pointers

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Course organization. Course introduction ( Week 1)

Pointers. Memory. void foo() { }//return

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Fundamentals of Programming Session 20

CS 237 Meeting 19 10/24/12

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Important Questions for Viva CPU

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

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

Exercise 1.1 Hello world

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Arrays and Pointers (part 1)

In Java we have the keyword null, which is the value of an uninitialized reference type

Final CSE 131 Winter 2010

Computer Programming: Skills & Concepts (CP) Strings

a data type is Types

Spring 2008 May 14 th, 2008 CS107 Midterm Solution

Tin học cơ sở 4$ Structures!

C-Refresher: Session 06 Pointers and Arrays

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

Introduction to C Language (M3-R )

Type Checking. Prof. James L. Frankel Harvard University

Low-Level C Programming. Memory map Pointers Arrays Structures

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

o Code, executable, and process o Main memory vs. virtual memory

20 Dynamic allocation of memory: malloc and calloc

POINTER & REFERENCE VARIABLES

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Variation of Pointers

PDS Class Test 2. Room Sections No of students

An application: foreign function bindings

ECE551 Midterm Version 1

Writing Functions in C

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CS201- Introduction to Programming Current Quizzes

CS 345. Functions. Vitaly Shmatikov. slide 1

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Outline. Pointers arithme.c and others Func.ons & pointers

Pointers. Pointer References

Dynamically Allocated Memory in C

Type Conversion. and. Statements

References and pointers

Unit IV & V Previous Papers 1 mark Answers

Lecture 07 Debugging Programs with GDB

Transcription:

Slide: 1 Generics

void swap (void *ap, void *bp, int size) { char temp[size]; memcpy (temp, ap, size); memcpy (ap, bp, size); memcpy (bp, temp, size); int main () { int i = 15; int j = 61; swap1 (&i, &j, sizeof (int)); double x = 3.14156; double y = 61.2534; swap1 (&x, &y, sizeof (double)); int p = 44; short q = 1; swap1 (&p, &q, sizeof (short)); Generic Swap char *s1 = strdup ("Fred"); char *s2 = strdup ("Wilma"); swap1 (&s1, &s2, sizeof (char *)); swap1 (s1, s2, sizeof (char *)); swap1 (s1, &s2, sizeof (char *)); Slide: 2

Linear Search int linsearch (int key, int array[], int sizeofarray) { int i; for (int i = 0; i < sizeofarray; i++) { if (array[i] == key) return i; return -1; int array[5] = {1, 2, -3, 44, -5; int key = 1; int status = linsearch (key, array, 5); Slide: 3

Generic Linear Search int genlinsearch (void *key, void *base, int sizeofarray, int elemsize) { int i; for (int i = 0; i < sizeofarray; i++) { void *elemaddr = (char *) base + i * elemsize; if (memcmp (elemaddr, key, elemsize) == 0) return i; return -1; int array[5] = {1, 2, -3, 44, -5; int key = 1; int status = genlinsearch (&key, array, 5, sizeof (int)); Slide: 4

Function Pointers: Recap int *gi(); () binds more tightly than *. *gi() is same as*(gi()) *(gi()) is of type int => gi() is a pointer to int, int (*hi)(); => gi is a function that returns pointer to int. (*hi)() is int => (*hi) is a function that returns int => hi is a pointer to a function that returns int Slide: 5

Understanding typecasts If you know how to declare a type, it is easy to write a cast for that type. Type Declaration Cast Pointer to function that returns int int (*fp)() int (*)() Pointer to function that returns nothing void (*fp)() void (*)() Slide: 6

Function pointers Variables which point to the address of a function We can implement call-backs Complicated syntax Use it only when you need it! Slide: 7

Function pointer: Example char ** fun() { static char * z = (char*)"merry Christmas :)"; return &z; int main() { char ** ptr = NULL; char ** (*fun_ptr)(); //declaration of pointer to the function fun_ptr = &fun; ptr = fun(); printf("\n %s \n Address of function = [%p]", *ptr, fun_ptr); printf("\n Address of first variable created in fun() = [%p]", (void*)ptr); cout<<endl; return 0; Slide: 8

More Generic Linear Search int genlinsearch (void *key,void *base,int sizeofarray, int elemsize, int (*cmpfunc) (void *, void *)) ) { int i; for (int i = 0; i < sizeofarray; i++) { void *elemaddr = (char *) base + i * elemsize; if (cmpfunc(elemaddr, key) == 0) return i; return -1; int integercmp (void *elem1, void *elem2) { int *ip1 = elem1; int *ip2 = elem2; return *ip1 - *ip2; int array[5] = {1, 2, -3, 44, -5; int key = 1; int status = genlinsearch (&key, array, 5, sizeof (int), integercmp); Slide: 9

Fill out the body of stringcmp int genlinsearch (void *key,void *base,int sizeofarray, int elemsize, int (*cmpfunc) (void *, void *)) ) { int i; for (int i = 0; i < sizeofarray; i++) { void *elemaddr = (char *) base + i * elemsize; if (cmpfunc(elemaddr, key) == 0) return i; return -1; int stringcmp (void *elem1, void *elem2) { char *s1 = * (char **) elem1; char *s2 = * (char **) elem2; return strcmp (s1, s2); char *array[5] = { Ab, F#, B, Gb, D ; char *key = Eb ; int status = genlinsearch (&key, array, 5, sizeof (char *), stringcmp); Slide: 10

With implicit casts int genlinsearch (void *key,void *base,int sizeofarray, int elemsize, int (*cmpfunc) (void *, void *)) ) { int i; for (int i = 0; i < sizeofarray; i++) { void *elemaddr = (char *) base + i * elemsize; if (cmpfunc(elemaddr, key) == 0) return i; return -1; int stringcmp (void *elem1, void *elem2) { char **s1 = elem1; char **s2 = elem2; return strcmp (*s1, *s2); char *array[5] = { Ab, F#, B, Gb, D ; char *key = Eb ; int status = genlinsearch (&key, array, 5, sizeof (char *), stringcmp); Slide: 11

Is this fine? int stringcmp (void *elem1, void *elem2) { char *s1 = elem1; char *s2 = elem2; return strcmp (s1, s2); char *array[5] = { Ab, F#, B, Gb, D ; char *key = Eb ; int status = genlinsearch (&key, array, 5, sizeof (char *), stringcmp); Slide: 12

int stringcmp (void *elem1, void *elem2) { char *s1 = elem1; char *s2 = elem2; return strcmp (s1, s2); Check this one!! char **notes = (char **) malloc (5 * sizeof (char *)); // Allocating 5 char pointers notes[0] = strdup ("Ab"); notes[1] = strdup ("F#"); notes[2] = strdup ("B"); notes[3] = strdup ("Eb"); notes[4] = strdup ("D"); char *favnote2 = strdup ("Eb"); status = genlinsearch (&favnote2, notes, 5, sizeof (char *), stringcmp); int status = genlinsearch (&key, array, 5, sizeof (char *), stringcmp); Slide: 13