Introduction
Pointers are variables that store an address. They are a fundamental mechanism in C that enable pass by address, allowing functions to work with the original variables rather than copies. Pointers are an efficient way to pass complex types into a function as we are only passing in their location. Aside from efficiency, pointers also allow a function to provide multiple results.
Learning Outcomes
After completing this chapter, students will be able to:
- Explain pointer related concepts by defining the relationships between pointers, variables and memory.
- Declare pointer variables using proper C syntax and understand that pointers store memory addresses of other variables
- Understand pass by value vs. pass by address and explain why pass by address is more efficient when passing structs to functions
- Pass structs to functions by address using the
&operator and recognize how this avoids duplicating struct data - Access struct data members through pointers using the
->operator and explain how it differs from the.operator - Use pointers to pass back multiple results from a function by dereferencing pointers with the
*operator to modify variables in the calling function - Distinguish between the address-of operator and dereference operator and apply each correctly in different contexts