COMP 206 Lecture Notes - Lecture 12: Function Pointer, Sbrk, C Standard Library

46 views8 pages

Document Summary

Plan for today: a few final thoughts about structs, grab-bag of final c topics, start the next section additional tools for software systems. Answer: not like this: struct s_student{ char grade = (cid:858)a(cid:859); i(cid:374)t stude(cid:374)tid = (cid:1006)6(cid:1004)(cid:1005); : but it is common to use this format: (cid:272)o(cid:374)st stru(cid:272)t s_stude(cid:374)t default = { (cid:858)a(cid:859), (cid:1004) }; stru(cid:272)t s_student = defaults; In both cases, it is common to write your own operation so you definitely get what you wanted. Why have function pointers: provides an additional level of abstraction you can pass around the pointer and use it to do different operations depending on context, example: sorting integers vs sorting strings int (*comparison)( void*, void* ); // comparison can point to either int compare_strings( void* a, void* b ){ return strcmp( (char*)a, (char*)b); } // or int compare_ints( void* a, void *b ) { return a!=b; : note: the two functions must have the same specification, but can differ in implementation!

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents