This week
• Pointers
– Pointer Variable Declarations and Initialization
– Pointer Operators
– Pointers to void
– Calling Functions by Reference
– Passing parameters by reference
– sizeof function
– Dynamic Memory Management
– Pointer Arithmetic
– Pointers and Arrays
– Pointers to Functions
Variables Revisited
• What actually happens when we declare variables?
char a;
• C reserves a byte in memory to store a.
• Where is that memory? At an address.
• Under the hood, C has been keeping track of variables
and their addresses.
Slide credit: B. Huang
Pointers
• We can work with memory addresses too. We can
use variables called pointers.
• A pointer is a variable that contains the address of a
variable.
• Pointers provide a powerful and flexible method for
manipulating data in your programs; but they are
difficult to master.
– Close relationship with arrays and strings
Benefits of Pointers
• Pointers allow you to reference a large data structure in a
compact way.
• Pointers facilitate sharing data between different parts of
a program.
– Call-by-Reference
• Dynamic memory allocation: Pointers make it possible to
reserve new memory during program execution.