Chapter 3. Pointers and Functions
Pointers contribute immensely to a function’s capability. They allow data to be passed and modified by a function. Complex data can also be passed and returned from a function in the form of a pointer to a structure. When pointers hold the address of a function, they provide a means to dynamically control a program’s execution flow. In this chapter, we will explore the power of pointers as used with functions and learn how to use them to solve many real-world problems.
To understand functions and their use with pointers, a good understanding of the program stack is needed. The program stack is used by most modern block-structured languages, such as C, to support the execution of functions. When a function is invoked, its stack frame is created and then pushed onto the program stack. When the function returns, its stack frame is popped off of the program stack.
When working with functions, there are two areas where pointers become useful. The first is when we pass a pointer to a function. This allows the function to modify data referenced by the pointer and to pass blocks of information more efficiently.
The second area is declaring a pointer to a function. In essence, function notation is pointer notation. The function’s name evaluates to the address of the function, and the function’s parameters are passed to the function. As we will see, function pointers provide additional capability to control the execution flow of a program.
In this section, we ...
Get Understanding and Using C Pointers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.