Pointers Flashcards
int *a
a is a pointer to a place where some int can be stored (in function prototype, it means “expect a to be the address of some int”
Why are function prototypes beneficial?
Helps in debugging by checking the types and counts of all parameters.
What does a function prototype do?
Acts as a function’s interface; A prototype declares the function name, its parameters, and its return type to the rest of the program prior to the function’s actual declaration.
Pointers pass by reference or value?
By reference
Why are pointers powerful?
Because it gives function access to the locations of its parameters
What’s the benefit/curse of pointers?
Gives you the ability to modify memory not in your own scope (outside your scope).
&x vs *x
GET the address vs GO there (reverse processes); & is the address-of operator while * is go-there operator
Strong pointer
“Keep this in the heap until I no longer point to it”
Weak pointer
“Keep this as long as someone else points to it strongly”
Nil
The value of an object pointer that does not point to anything (value is 0)
All pointers initialize to nil (zero)
I.e. All instance variables that are pointers to objects start out with zero
Sending messages to an obj that is nil returns zero…
int i = [obj methodWhichReturnsAnInt] // i is zero if obj is nil
Why us pointers? Reason 1
Allow us to pass around anything we want around in memory
H
Give me access to another functioms’s memory