CS112_26_Slides-FunctionPtrs

CS112_26_Slides-FunctionPtrs - Function pointers Yinglin...

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

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Function pointers Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Main points Pointers to functions can be interpreted as data values in C Use function pointers in the implementation of callback function
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Function pointers Up to now, we would write two separate functions to print these tables, since they do different computations for the second column. CS112 Fundamentals of Programming Abstractions Function pointers What we would prefer is to write one function that prints tables, and provide the function that does the calculation to it as an argument. This allows PrintTable to be more general. It also results in a better decomposition, since it separates the notion of printing the table from the particular computation that will be used. PrintTable(start, end, function );
Background image of page 2
CS112 Fundamentals of Programming Abstractions Function pointers Here is what we would like to do, and we can in fact do it in C. We just need to learn some new syntax so that we can pass arguments like this. CS112 Fundamentals of Programming Abstractions Function pointers What we haven't seen yet is how to specify the type of the functional argument in the prototype or function heading of the function that will receive it.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Programming Abstractions Function pointers This is what the designers of C were thinking when they came up with the syntax. CS112 Fundamentals of Programming Abstractions Function pointers To compile the code when you call the function fn inside PrintTable , the compiler needs to know all of these things. For example, if it doesn't know what type of value the function returns, then it wouldn't know whether a type conversion was necessary if you assigned the result of the function to a variable. So, the syntax needs to give us a way to specify this information.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2010 for the course CS 112 taught by Professor Yinlinwang during the Spring '08 term at Shanghai Jiao Tong University.

Page1 / 16

CS112_26_Slides-FunctionPtrs - Function pointers Yinglin...

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

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