Professional Documents
Culture Documents
Learning Goals:
* Motivation
* Pointer as an Abstract Data Type
- Attributes and value domains
- Operators (malloc, free, calloc, realloc)
* Visualizing pointers w/ box-pointer diagrams
- More Operators: Assignment, Comparison, Initialization
- Yet More Operators (pointer arithmetic)
* What are Pointers used for in C?
- Dynamic Data-Structures, array, string,
- result parameters
*
-
Pointers ADT
* of base type T
Operations:
* address (&)
* dereference (*)
* assignment
* relational, e.g. ==, !=
* pointer arithmetic
* memory allocation, deallocation
point1 = &more_data;
your_point = point1;
*your_point = 17;
*my_point = 33;
}
Draw memory diagram to explain the code.
Memory allocation/deallocation
Memory allocation/deallocation
1.0
1.0
1111 M
id
s
t
a r
y
name
int main()
{
pt = ar + 2; /* pt point to ar[2] */
for (index = 0; index < 4; index++) [
ar[index] = (double)( 3 * index );
}
}
Arrays are implemented with Pointers
* Elements are contiguous in memory
* ar = const pointer to first element (i.e.ar[0])
* ar[index] computes ar + index * size(element)
* same as *(ar + index)
10
11
Parameter Types
12
Arrays of strings
int main(int argc, char *argv[]); /*Example 1.7, pp. 17 */
char ** makeargv(char *s); /*Example 1.8, pp. 18 */
char **myargv; /*Example 1.9, pp. 18 */
Parameter Passing
13
14
"
Comparing Implementations
element
lChild
rChild
Q
G
0 1 2 3 4 5 6 7 8
Q G S D P R X
1 3 5 -1
2 4 6 -1
S
Q
X
G
X
P
15
16
'