Title: Pointers and Arrays
1Pointers and Arrays
2C is Strongly Typed
3Activation Stacks?
4Pointers are like jumps, leading wildly from one
part of the data structure to another. Their
introduction into high-level languages has been a
step backwards from which we may never recover.
Tony Hoare Famous British computer scientist
5Pointers are cool!
Jim Greenlee Famous American computer scientist
6Recall
- A variable associates two things
- A symbolic name
- A value
- In actual implementation in computing, a variable
typically associates a symbolic name, a physical
address and a value - Pointers are a special kind of variable where the
value stored is the physical address of some
other variable or data.
7Pointers
- int i
- int ip
- Pointer Variable that contains address of
another variable. - A pointer is a data object which is separate from
what it points to. - ip is a pointer to an integer
- It is not necessarily pointing to an integer
8The Mysterious
- Declaration
- int ip
- The name of the variable being declared is ip
- Per K R meaning is that when ip gets
dereferenced i.e. ip, the item referred to is an
int. - Use (i.e. dereferencing)
- ip is the value of the pointer
- ip is the value of what it is pointing at.
- Here the is an operator
9Pointers
Name
Contents
Code int i
i
10Pointers
Name
Contents
Code int i int ip
i
ip
11Pointers
Name
Contents
Code int i int ip i 42
i
42
ip
12Pointers
Name
Contents
Code int i int ip i 42 ip 84
i
42
ip
ERROR!!! Bad Mojo!!!
13Pointers
Name
Contents
Code int i int ip i 42 ip i
i
42
ip
Address of Operator
14Pointers
Name
Contents
Code int i 42 int ip i i 42 ip
i ip 84
i
84
ip
15Pointers
Name
Contents
Code int i int ip i 42 ip i ip i
i
??????????
ip
NO!!!
16Questionint ip NULLis equivalent to?
17Notice the inconsistency
- int i 42
- int pi i
- pi 1036
- int i
- int pi
- i 42
- pi i
- pi 1036
18Translation!
19Pointers
- Powerful and dangerous
- No runtime checking (for efficiency)
- Bad reputation
- Java attempts to remove the features of pointers
that cause many of the problems hence the
decision to call them references - No address of operators
- No dereferencing operator (always dereferencing)
- No pointer arithmetic
20Question?
- int x 3
- int y 72
- int px x
- int py y
- px 7
- py px
- x 12
- printf("d d\n", px, py)
- What is the output?
- 3 72
- 72 3
- 7 12
- 12 7
- 3 3
- 72 72
- 12 12
- 12 72
- 72 12
21Questions?
22Suppose...
- Our beloved TA tells us that video memory is
located at address 0x6000000 - How can we store information in the video buffer?
- Recall...in Mode 3 the video buffer is how big?
240x160 pixels - 38,400 shorts
- 19,200 ints
- 76,800 bytes
23What we need is a pointer!!!
- We know the address 0x6000000
- unsigned short videoBuffer
- videoBuffer 0x6000000
- Okay?
24Better
- unsigned short videoBuffer
- videoBuffer (unsigned short )0x6000000
25Typical
- unsigned short videoBuffer
- (unsigned short )0x6000000
- or even
- u16 videoBuffer (u16 )0x6000000
26Typical
- u16 videoBuffer (u16 )0x6000000
- Declare a pointer named videoBuffer that will
hold the address of an unsigned 16 bit integer
(i.e. an unsigned short). - Initialize videoBuffer to hold the hexidecimal
address 6,000,000 which is the address of an
unsigned 16 bit integer (i.e. an unsigned short).
27Another Example
28Swap
- Assignment Write a function that will swap two
integers - First try
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
-
29What's wrong?
- We call it like this
- int x 42
- int y 84
- swap(x, y)
30Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
y
84
Stack Frame for main
x
42
stack
31Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
Stack Frame for swap
b
84
a
42
y
84
Stack Frame for main
x
42
stack
32Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
84
a
42
y
84
Stack Frame for main
x
42
stack
33Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
84
a
84
y
84
Stack Frame for main
x
42
stack
34Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
42
a
84
y
84
Stack Frame for main
x
42
stack
35Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
pop
y
84
Stack Frame for main
x
42
stack
36The True Way
37Swap
- Assignment Write a function that will swap two
integers - Last try
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
-
38Now it works
- We call it like this
- int x 42
- int y 84
- swap(x, y)
39Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
y
84
Stack Frame for main
x
42
stack
40Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
Stack Frame for swap
b
a
y
84
x
42
stack
41Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
a
y
84
x
42
stack
42Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
a
y
84
x
84
stack
43Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
t
42
Stack Frame for swap
b
a
y
42
x
84
stack
44Trace
- int x 42
- int y 84
- swap(x, y)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
pop
y
42
Stack Frame for main
x
84
stack
45Another way of looking at it
- int x 42
- int y 84
- int px x
- int py y
- swap(px, py)
- void swap(int a, int b)
-
- int t
- t a
- a b
- b t
-
46Questions?
47Arrays
- int ia6
- Allocates consecutive spaces for 6 integers
- How much space is allocated?
-
48Arrays
- int ia6
- Allocates consecutive spaces for 6 integers
- How much space is allocated?
- 6 sizeof(int)
- Also creates ia which is effectively a constant
pointer to the first of the six integers - What does ia4 mean?
ia
49Arrays
- int ia6
- Allocates consecutive spaces for 6 integers
- How much space is allocated?
- 6 sizeof(int)
- Also creates ia which is effectively a constant
pointer to the first of the six integers - What does ia4 mean?
- Multiply 4 by sizeof(int). Add to ia and
dereference yielding
ia4
ia
50Arrays
- int ia6
- Note ia ? ia0
- Never say, "Pointers and arrays are exactly the
same thing!!! - int ip
- ip ia / Okay /
- ia ip / Illegal /
51sizeof
- Compile time operator
- Two forms
- sizeof object
- sizeof ( type name )
- Returns the size of the object or the size of
objects of type name in bytes - Note Parentheses can be used in the first form
with no adverse effects
52sizeof
- int i
- if sizeof(int) 4
- then sizeof(i) 4
53sizeof
- On a typical 32 bit machine...e.g. GBA
- int ip
- short sp
- char cp
- sizeof(int) ? 4
- sizeof(ip) ? 4
- sizeof(ip) ? 4
- sizeof(short) ? 2
- sizeof(sp) ? 2
- sizeof(sp) ? 4
- sizeof(char) ? 1
- sizeof(cp) ? 1
- sizeof(cp) ? 4
-
Not the same thing!!!
54sizeof
- And...
- int ia6
- sizeof(ia) ? 24
55Arrays
Dont you ever use sizeof like this!!!
ia
- ia4 means (ia 4)
- which means (ia 4 sizeof(int))
- Or (ia 4 sizeof(ia))
56Pointer Arithmetic
- Note on the previous slide when we added the
literal 4 to a pointer it actually gets
interpreted to mean - 4 sizeof(thing being pointed at)
- This is why pointers have associated with them
what they are pointing at!
57Pop Quiz
- u16 shrtArr5 0, 1, 2, 3, 4
- (shrtArr 2) 17
- What does the array contain now?
- Was this the same as
- shrtArr2 17
- ???
58Arrays
0
1
2
3
4
5
ia
- Array elements are numbered like this since
that's how the pointer arithmetic works out!
59Arrays
0
1
2
3
4
5
42
ia
ia4 42
60Fun with C
- int ia6
- ia4 42
- is the same as
- (ia 4) 42
- and since addition is commutative
- (4 ia) 42
- would imply that
- 4ia 42
- should work.
- Does it? Is it a good idea?
- Works, bad idea
- Works, good idea
- Doesn't work
61Address Operators
- Have a variable and want the address of it.
- Have address (or pointer) and want value of
variable that it's pointing at.
Know this!
62Pop Quiz
- define MAX 6
- int iaMAX
- int ip
- ip ia / 1 /
- ia ip / 2 /
- ip2 87 / 3 /
- ip refers to the first element of ia
- ip increments ip by how much?
- ip now refers to what?
Which is illegal?
63Pop Quiz
- define MAX 6
- int iaMAX
- int ip
- ip ia / 1 /
- ia ip / 2 /
- ip2 87 / 3 /
- ip refers to the first element of ia
- ip increments ip by how much?
- ip now refers to what?
- ia1
Which is illegal?
64More pointer arithmetic
- int i
- int iaMAX
- for(i 0 i lt MAX i)
- iai 0
- int ip
- int iaMAX
- for(ip ia ip lt ia MAX ip)
- ip 0
Sometimes pointer arithmetic is faster than
array manipulation
65Pointers to Pointers?
Name
Contents
Code int i
i
66Pointers to Pointers?
Name
Contents
Code int i int ip
i
ip
67Pointers to Pointers?
Name
Contents
Code int i int ip int ipp
i
ip
ipp
68Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42
i
42
ip
ipp
69Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip i
i
42
ip
ipp
70Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip
i ip 84
i
84
ip
ipp
71Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip
i ip 84 ipp ip
i
84
ip
ipp
72Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip
i ip 84 ipp ip ipp 22
i
22
ip
ipp
73Hungarian Notation
- Invented by Charles Simonyi
- "Some people think it's the best thing since
structured programming others hate Hungarian
with a passion." - Incredibly simplified version
- Include in the name of every pointer as many p's
as there are 's - Example
- int pint
- char ppchr
- Then when dereferencing, each cancels a p
- pint refers to an int
- ppchr refers to a pointer to a char
74Don't confuse
- Arrays
- int ia10
- func(ia)
- Arrays of pointers
- char names13
- func(names)
- Pointers to pointers
- Node phead
- func(phead)
- As formal parameter
- int iarr
- int iarr
- As formal parameter
- char nms
- char nms
- Pointers to pointers
- Node ppcurr
75More fun with swap?
- int arr1 1, 2, 3
- int arr2 9, 8, 7, 6
- int p1 arr1
- int p2 arr2
- / Magic function call occurs here /
- for(i0 ilt4 i)
- printf("d ", p1i)
-
- for(i0 ilt3 i)
- printf("d ", p2i)
9 8 7 6 1 2 3
76More fun with swap?
- int arr1 1, 2, 3
- int arr2 9, 8, 7, 6
- int p1 arr1
- int p2 arr2
- swap_pointers(p1, p2)
- for(i0 ilt4 i)
- printf("d ", p1i)
-
- for(i0 ilt3 i)
- printf("d ", p2i)
9 8 7 6 1 2 3
77- void swap_pointers(int a, int b) / Choice
1 / - int t
- t a
- a b
- b t
-
- void swap_pointers(int a, int b) / Choice
2 / - int t
- t a
- a b
- b t
-
- void swap_pointers(int a, int b) / Choice
3 / - int t
- t a
- a b
- b t
78Questions?
79Arrays of Pointers
- char month_name(int n)
-
- static char name
- "Illegal month",
- "January", "February", "March",
- "April", "May", "June",
- "July", "August", "September",
- "October", "November", "December"
-
- return (nlt1 ngt12)? name0 namen
80Arrays of Pointers
- A block of memory (probably in the constant area)
is initialized like this
Illegal month\0January\0February\0March\0April\0Ma
y\0June\0July\0August\0September\0October\0Novembe
r\0December\0
81Arrays of Pointers
- A block of memory (probably in the constant area)
is initialized like this
Illegal month\0January\0February\0 March\0April\0M
ay\0June\0July\0August\0 September\0October\0Novem
ber\0December\0
82Arrays of Pointers
- An array is created in the static area which will
hold 13 character pointers
Illegal month\0January\0February\0 March\0April\0M
ay\0June\0July\0August\0 September\0October\0Novem
ber\0December\0
83Arrays of Pointers
- The pointers are initialized like so
Illegal month\0January\0February\0 March\0April\0M
ay\0June\0July\0August\0 September\0October\0Novem
ber\0December\0
84Arrays of Pointers
- So when the function is called
- printf("The third month is s\n", month_name(3))
- The line return (nlt1 ngt12)? name0 namen
- Gives us back the address (a pointer to) the 'M'
Illegal month\0January\0February\0 March\0April\0M
ay\0June\0July\0August\0 September\0October\0Novem
ber\0December\0
85Questions?
86Recall
42
Address calculation 2 sizeof(ia) ia Access
is by dereferencing (2 sizeof(ia) ia)
Remember! You don't type in the sizeof part!
87What happens?
42
84
Address calculation 8 sizeof(ia) ia
Remember! You don't type in the sizeof part!
88Questions?
- Stop in the name of love!
89Multi-Dimensional Arrays
- How did multidimensional arrays work in Java?
- More to the point...how do they work in Fortran?
90Declaration
Number of Columns
Number of Rows
Address
Type
Declaration at compile time i.e. size must be
known
91How does a two dimensional array work?
How would you store it?
92How would you store it?
93Advantage
- Using Row Major Order allows visualization as an
array of arrays - ia1
- ia12
0,0
0,1
0,2
0,3
1,0
1,1
1,3
2,0
2,1
2,2
2,3
1,2
1,2
0,0
0,1
0,2
0,3
1,0
1,1
1,3
2,0
2,1
2,2
2,3
94What's the output?
- include ltstdio.hgt
- include ltstdlib.hgt
- int main()
-
- int a57
- printf("sizeof a d\n", sizeof a)
- printf("sizeof a3 d\n", sizeof a3)
- printf("sizeof a34 d\n", sizeof a34)
- return EXIT_SUCCESS
?
95What's the output?
- include ltstdio.hgt
- include ltstdlib.hgt
- int main()
-
- int a57
- printf("sizeof a d\n", sizeof a)
- printf("sizeof a3 d\n", sizeof a3)
- printf("sizeof a34 d\n", sizeof a34)
- return EXIT_SUCCESS
?
96What's the output?
- include ltstdio.hgt
- include ltstdlib.hgt
- int main()
-
- int a57
- printf("sizeof a d\n", sizeof a)
- printf("sizeof a3 d\n", sizeof a3)
- printf("sizeof a34 d\n", sizeof a34)
- return EXIT_SUCCESS
?
97Recall
- One Dimensional Array
- int ia6
- Address of beginning of array
- ia ? ia0
- Two Dimensional Array
- int ia36
- Address of beginning of array
- ia ? ia00
- also
- Address of row 0
- ia0 ? ia00
- Address of row 1
- ia1 ? ia10
- Address of row 2
- ia2 ? ia20
98Element Access
- Given a row and a column index
- How to calculate location?
- To skip over required number of rows
- row_index sizeof(row)
- row_index Number_of_columns sizeof(arr_type)
- This plus address of array gives address of first
element of desired row - Add column_index sizeof(arr_type) to get actual
desired element
99Element Access
- Element_Address
- Array_Address
- Row_Index Num_Columns Sizeof(Arr_Type)
- Column_Index Sizeof(Arr_Type)
- Element_Address
- Array_Address
- (Row_Index Num_Columns Column_Index)
- Sizeof(Arr_Type)
100What if array is stored in Column Major Order?
- Element_Address
- Array_Address
- (Column_Index Num_Rows Row_Index)
- Sizeof(Arr_Type)
101Don't confuse
- Arrays
- int ia10
- func(ia)
- Arrays of pointers
- char names13
- func(names)
- Pointers to pointers
- Node phead
- func(phead)
- Multidimensional arrays
- int ia345
- func(ia)
- As formal parameter
- int iarr
- int iarr
- As formal parameter
- char nms
- char nms
- Pointers to pointers
- Node ppcurr
- Multidimensional arrays
- int iar45
- int iar / Note /
Note Cannot use as a multidimensional array
102Multidimensional Example
- void tester(int arr45)
-
- int main()
-
- int ia345
- int ib845
- int ic
- ic ia
-
- tester(ia)
- tester(ib)
- tester(ic)
- return 0
103What's Up?
- Why int arr45???
- Consider a one dimensional array
- If asked to determine the address of a given
element does one need to know the size of the
array? - Consider a 2D array
- What is needed to calculate the address of a
given element (i,j)? - offset i columns j
104Now think about
int a
105Now think about
int a5
106Now think about
int a45
107Now think about
int a345
108Offset to aijk?
int a345 slicesrowscolumns
offset (i rows columns) (j columns)
k
109STOP
- View the remainder of this presentation after the
Dynamic Allocation presentation
110Compare and Contrast
- There are two different ways that
multidimensional arrays could be implemented in
C. - A Static implementation which is more efficient
in terms of space and probably more efficient in
terms of time. - A Dynamic implementation which is more flexible
in terms of run time definition of array size but
is arguably more complicated to understand
1116
1125 x 6
1134 x 5 x 6
1143 x 4 x 5 x 6
115Static Implementation
- int arr3456
- / initialization here /
- someFunction(arr)
- void someFunction(int array456)
-
- / use array normally /
- array1234 1234
116One could also...
- int arr3456
- / initialization here /
- someFunction((int )arr, 3, 4, 5, 6)
- void someFunction(int array,
- int s1, int s2, int s3, int s4)
-
- / use array normally /
- array1s2s3s4 2s3s4 3s4 4 1234
117Dynamic Implementation
118Pictorially
119Pictorially
?
120Pictorially
?
rows
121Pictorially
cols
rows
122- int alloc_array(int rows, int cols)
- int i
- int retval NULL
- retval malloc(rowssizeof(retval))
- / Handle error here /
- for(i0 i lt rows i)
- retvali malloc(colssizeof(retval))
- / Handle error here /
-
- return retval
-
- int main()
- int r,c
- int arr alloc_array(3,4)
- arr23 17
- ...
123- int alloc_array(int rows, int cols)
- int i
- int retval NULL
- retval malloc(rowssizeof(retval))
- if(NULL retval)
- return NULL
-
- for(i0 i lt rows i)
- retvali malloc(colssizeof(retval))
- if(NULL retvali)
- int ierror
- for(ierror 0 ierror lt i ierror)
- free(retvalierror)
-
- free(retval)
- return NULL
-
-
- return retval
124arr23 17
cols
17
rows
125Questions?
126Question?
- I understand clearly how to dynamically allocate
a two dimensional array - Strongly agree
- Agree
- Neutral
- Disagree
- Strongly disagree
127Dynamic Implementation
128DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
- for(i0 ilt3 i)
- pppparri malloc(4sizeof(pppparr))
- for(j0 jlt4 j)
- pppparrij malloc(5sizeof(pppparr))
- for(k0 klt5 k)
- pppparrijk
-
malloc(6sizeof(pppparr)) - for(m0 mlt6 m)
- pppparrijkm
- i1000
j100 k10 m -
-
-
-
- testfun(pppparr)
Error checking omitted...DDTAH
129DynamicImplementation
- void testfun(int a)
-
- printf("d\n", a1234)
- return
130DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
-
131What's Going On?
int
pppparr
int
132DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
- for(i0 ilt3 i)
- pppparri malloc(4sizeof(pppparr))
-
133What's Going On?
int
pppparr
int
int
134DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
- for(i0 ilt3 i)
- pppparri malloc(4sizeof(pppparr))
- for(j0 jlt4 j)
- pppparrij malloc(5sizeof(pppparr))
-
135What's Going On?
int
pppparr
int
int
int
136DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
- for(i0 ilt3 i)
- pppparri malloc(4sizeof(pppparr))
- for(j0 jlt4 j)
- pppparrij malloc(5sizeof(pppparr))
- for(k0 klt5 k)
- pppparrijk
-
malloc(6sizeof(pppparr)) -
137What's Going On?
int
pppparr
int
int
int
int
138DynamicImplementation
- int main()
- int i, j, k, m
- int pppparr
- pppparr malloc(3sizeof(pppparr))
- for(i0 ilt3 i)
- pppparri malloc(4sizeof(pppparr))
- for(j0 jlt4 j)
- pppparrij malloc(5sizeof(pppparr))
- for(k0 klt5 k)
- pppparrijk
-
malloc(6sizeof(pppparr)) - for(m0 mlt6 m)
- pppparrijkm
- i1000
j100 k10 m -
-
-
-
- testfun(pppparr)
139What's Going On?
int
pppparr
int
int
int
int
140DynamicImplementation
- void testfun(int a)
-
- printf("d\n", a1234)
- return
141What's Going On?
a
int
pppparr
int
int
int
int
142DynamicImplementation
- void testfun(int a)
-
- printf("d\n", a1234)
- return
143What's Going On?
int
pppparr
int
int
int
int
144DynamicImplementation
- void testfun(int a)
-
- printf("d\n", a1234)
- return
145What's Going On?
int
pppparr
int
int
int
int
146DynamicImplementation
- void testfun(int a)
-
- printf("d\n", a1234)
- return
147What's Going On?
int
pppparr
int
int
int
148Summary
- Static
- Must be known at compile time
- Only store data
- index calculation...simple math
- Must either know n-1 dimensions to pass array to
function - or
- Do calculation yourself
- Still no bounds checking!
- Dynamic
- Can allocate space at run time given dimensions
- Must store substantial quantity of pointers
- Can pass pointer to array
- But function still doesn't know size
- Still no bounds checking
149Questions?
150(No Transcript)