{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect19

# lect19 - CMSC 216 Introduction to Computer Systems Lecture...

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

CMSC 216 Introduction to Computer Systems Lecture 19 Function Pointers & Data Representation Jan Plane & Pete Keleher {jplane,als}@cs.umd.edu Administrivia Project 5 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 2

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

View Full Document
F UNCTION P OINTERS Section 13.3, Reek 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 ) 4
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" */ 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); 6

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

View Full Document
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 *); 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 * 7 D ATA R EPRESENTATION Parts of Sections 2.1-2.4, Bryant and O'Hallaron 8
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 9 ASCII Represents normal characters on US keyboards A - Z (the characters numbered 65-90) a - z (97-122) 0 - 9 (48-57) – space (32) – control characters (0-31, 127) the first 26 (after 0) of the 33 ASCII control characters have names Ctrl-A - Ctrl-Z for example, ASCII character 13, Ctrl-M, is CR (carriage return) ( \r in C); ASCII char. 9, Ctrl-I, is HT (horizontal tab) ( \t in C) – punctuation: [email protected]#\$%^&*()_+-=[]{}|\;:"'<>?,./ (the remaining characters) The UNIX command " man ascii " shows the ASCII character set 10

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

lect19 - CMSC 216 Introduction to Computer Systems Lecture...

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

View Full Document
Ask a homework question - tutors are online