Lecture 24: Separate Compilation and Pointers
Wednesday, April 24, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 6.1-6.2 and 8.5
Homework Due: Homework 11
Homework Assigned: Homework 12
Classwork Assigned: Classwork 13
Topics Covered:
- Homework 11 questions?
- Upcoming Homework 12 questions?
- Quiz 4 - Results
- Reminder about how final grade is calculated
- Go back and finish separate compilation
- Pointers (aka How to Get Multiple Values Back from Functions)
- Remember variables are stored at a defined location in memory
- Normally when we pass arguments to functions, the computer allocates a new memory region
- To get a value back from a function, we used to have to set a variable equal to the return value
- Pointers allow us to pass a pointer to the memory location of a variable to a function
- The function can then modify the same memory location occupied by the original variable
- Pointers are one of the more complicated parts of C, so it will take a while to "get it"
- Pointer syntax
- A normal integer variable "int x;"
- A pointer to an integer "int *y;"
- Getting the address of a normal variable "&x"
- Setting a integer pointer to point to an integer: "int *y = &x;"
- A program example
- A program using pointers to pass back a value
- Operators used with pointers
- * - dereference operator
- & - address of operator