Pointer Cheat Sheet
This page is based on information that appeared on the tear-out Cheat Sheet from C For Dummies, Volume II.
Pointer Pointers
- A pointer must always be of the same data type as the variable it's referencing.
- Though pointers are declared with an asterisk they're not always used with an asterisk.
- The asterisk is the unary
*
operator. It is not the*
multiplication operator. - Pointers must be initialized before they can be used!
- Initialize a pointer by assigning it to a variable; the variable must be of the same data type as the pointer.
- To assign a pointer to a variable, use an ampersand (
&
) operator on variable's. - The address-of unary operator
&
is not the same as the bitwise&
AND operator. - To assign a pointer to an array, do not use the ampersand:
- To assign a pointer to an array element, use the ampersand:
- Without an asterisk, an initialized pointer holds a memory address.
- With an asterisk, an initialized pointer references the value stored at its address.
m_address = &memory;
s_address = string;
element = &string[2];
Typical Pointer Setup and Use
First, create a pointer of the proper type:
float *f;
Second assign it to a variable's memory location, assuming both variables are of the same data type:
f = &boat;
Finally, use the pointer:
printf("%.0f",*f);
- Without an asterisk, the pointer references a memory location.
- With an asterisk, the pointer references the value at that memory location.
- Always use the same data type as the variables the pointer examines: floats for floats, ints for ints, and so on.
- Remember: initialize a pointer before you use it! Set the pointer equal to the address of some variable in memory.
Pointers, Parenthesis and Math
Pointer Thing | Memory Address | Memory Contents |
---|---|---|
p |
Yep | Nope |
*p |
Nope | Yep |
*p++ |
Incremented after value is read | Unchanged |
*(p++) |
Incremented after value is read | Unchanged |
(*p)++ |
Unchanged | Incremented after it's used |
*++p |
Incremented before value is read | Unchanged |
*(++p) |
Incremented before value is read | Unchanged |
++*p |
Unchanged | Incremented before it's used |
++(*p) |
Unchanged | Incremented before it's used |
p*++ |
Not a pointer | Not a pointer |
p++* |
Not a pointer | Not a pointer |
The ++ operator is used above for illustration purposes.
A tip: Use parenthesis to isolate part of the pointer problem and the answer will always work out the way you intended.
Pointers and Array Brackets
Array Notation | Pointer Equivalent |
---|---|
array[0] |
*a |
array[1] |
*(a+1) |
array[2] |
*(a+2) |
array[3] |
*(a+3) |
array[x] |
*(a+x) |
Pointer to Pointer ( **) Notation
Doodad | What It Is | Data Type |
---|---|---|
*array[] |
An array of pointers | Array |
**array |
An array of pointers | Array (double pointer) |
array+1 |
An address | Pointer |
*(array+1) |
Contents of address, what lives there | String/pointer |
*(*array+n) |
Character n in the string at *array (where 0 is the first character) |
Character |
Copyright © 1997-2024 by QPBC.
All rights reserved