19 Function Pointers and Data Representation

19 Function Pointers and Data Representation -...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
!"#!"## # CMSC 216 Introduction to Computer Systems Lecture 19 Process Control and System-Level I/O Jan Plane & Pete Keleher {jplane,als}@cs.umd.edu Administrivia • Project 5 out – don’t procrastinate • Read Chapter 10 • Don’t forget course projects policy – have to make a good faith effort on all projects before end of semester – that means submit a version that works on at least 75% of public tests CMSC 216 - Wood, Sussman, Herman, Plane 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
!"#!"## % F UNCTION P OINTERS Section 13.3, Reek CMSC 216 - Wood, Sussman, Herman, Plane 3 Function Pointers • Each function is located somewhere in memory; this means we can create a pointer to it • Declared like this: void (*fp)(int); fp is a pointer to a function that returns void and has a single parameter (which is an int ) int *(*fp2)(char *, int); fp2 is a pointer to a function that returns a pointer to an int , and has 2 parameters (a pointer to char , and an int ) CMSC 216 - Wood, Sussman, Herman, Plane 4
Background image of page 2
!"#!"## Using function pointers void print_decimal(unsigned int i) { printf("%u\n", i); } void print_hex(unsigned int i) { printf("%x\n", i); } void print_octal(unsigned int i) { printf("%o\n", i); } ... void (*fp)(unsigned int); fp = print_hex; fp(16); /* prints "10" */ fp = &print_octal; fp(16); /* prints "20" */ fp = print_decimal; (*fp)(16); /* prints "16" */ CMSC 216 - Wood, Sussman, Herman, Plane 5 Using typedef with function pointers • To make things a bit more clear, we can use typedef to create a specific function pointer type • Example: typedef char *(*Str_func)(char *); char *strdup(char *str) { . .. } ... Str_func sf = strdup; char *copy = sf(str); CMSC 216 - Wood, Sussman, Herman, Plane 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
!"#!"## ! Understanding complex declarations • Even people who've programmed in C for a long while may have trouble deciphering this declaration: int *(*f[8])(char *); • The program cdecl can be of use here: $ cdecl Type `help' or `?' for help cdecl> explain int *(*f[8])(char *); declare f as array 8 of pointer to function (pointer to char) returning pointer to int • In other words, f is an array containing 8 function pointers, each of which can point to a function that takes a char * as an argument and returns an int * CMSC 216 - Wood, Sussman, Herman, Plane 7 D ATA R EPRESENTATION Parts of Sections 2.1-2.4, Bryant and O'Hallaron CMSC 216 - Wood, Sussman, Herman, Plane 8
Background image of page 4
!"#!"## ' Representing characters • We need: – to be able to represent common characters – to have standards so computers can interoperate • Common formats – ASCII • is the most commonly used character code • uses 7 bits for characters (stored in 8 bits normally) – EBCDIC • an 8-bit code, used now only by some IBM mainframes – UNICODE • a family of encodings - 8, 16, and 32 bits per character • allows a greater variety of characters • is able to represent virtually any character in use today in any language, and some no longer in use CMSC 216 - Wood, Sussman, Herman, Plane
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 12

19 Function Pointers and Data Representation -...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online