Thinking about Addresses
Why would you want to?
Indirection
Working with addresses
Storing Addresses
Using a Pointer
All Types Can Have Pointers
I’m a pointer to a char:
char c = ‘x’;
char * cp = &c;
I’m a pointer to a float:
float f = 22.7;
float * fp = &f;
I’m a pointer to a short
short s = -192;
short * sp = &s;
long * p = &val;
long ** pp = &p;
Pointers to Pointers
int a = 5;
int b = 10;
int * pa = &a;
int * pb = &b;
int ** ppa = &pa;
int ** ppb = &pb;
Type Operator Precedence
A Pointer to Nowhere
What are They Good For?
Pass-By-Reference
How To Segfault
Still Pass-By-Value Underneath
Returning a Pointer
Pointers as Return Values
Poking Around in Memory
Type Matters … Sometimes
char *cp = &c; –> These look good.
float *fp = &f;
short *sp = &s;
fp = &c; -> But here, are you sure you know what you’re doing?
sp = &f;
cp = &s;
* A short pointer isn’t the same as a float
pointer.
Pointers and Size
Fun with Pointer Types
Sense…
*px = 35; // Copy 35 into the value pointed to by px
*px = b; // Copy the value of b into the value px
// points to.
*px = *py; // Copy the value py points to into the
// value px points to.
px = &b; // Copy the address of b into px
px = py; // Make px point to the same thing as py
Nonsense
Precedence
On doc