Pointers and Arrays - PowerPoint PPT Presentation

1 / 150
About This Presentation
Title:

Pointers and Arrays

Description:

Pointers are like jumps, leading wildly from one part of ... Bad Mojo!!! Pointers. Name. Contents. i. 42. ip. Code. int i; int *ip; i = 42; ip = &i; Address of ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 151
Provided by: BillL161
Category:
Tags: arrays | mojo | pointers

less

Transcript and Presenter's Notes

Title: Pointers and Arrays


1
Pointers and Arrays
2
C is Strongly Typed
3
Activation Stacks?
4
Pointers 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
5
Pointers are cool!
Jim Greenlee Famous American computer scientist
6
Recall
  • 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.

7
Pointers
  • 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

8
The 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

9
Pointers
Name
Contents
Code int i
i
10
Pointers
Name
Contents
Code int i int ip
i
ip
11
Pointers
Name
Contents
Code int i int ip i 42
i
42
ip
12
Pointers
Name
Contents
Code int i int ip i 42 ip 84
i
42
ip
ERROR!!! Bad Mojo!!!
13
Pointers
Name
Contents
Code int i int ip i 42 ip i
i
42
ip
Address of Operator
14
Pointers
Name
Contents
Code int i 42 int ip i i 42 ip
i ip 84
i
84
ip
15
Pointers
Name
Contents
Code int i int ip i 42 ip i ip i
i
??????????
ip
NO!!!
16
Questionint ip NULLis equivalent to?
  • Choice 1
  • int ip
  • ip NULL
  • Choice 2
  • int ip
  • ip NULL

17
Notice the inconsistency
  • int i 42
  • int pi i
  • pi 1036
  • int i
  • int pi
  • i 42
  • pi i
  • pi 1036

18
Translation!
  • int pi i
  • int pi
  • pi i

19
Pointers
  • 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

20
Question?
  • 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

21
Questions?
22
Suppose...
  • 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

23
What we need is a pointer!!!
  • We know the address 0x6000000
  • unsigned short videoBuffer
  • videoBuffer 0x6000000
  • Okay?

24
Better
  • unsigned short videoBuffer
  • videoBuffer (unsigned short )0x6000000

25
Typical
  • unsigned short videoBuffer
  • (unsigned short )0x6000000
  • or even
  • u16 videoBuffer (u16 )0x6000000

26
Typical
  • 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).

27
Another Example
28
Swap
  • 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

29
What's wrong?
  • We call it like this
  • int x 42
  • int y 84
  • swap(x, y)

30
Trace
  • 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
31
Trace
  • 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
32
Trace
  • 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
33
Trace
  • 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
34
Trace
  • 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
35
Trace
  • 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
36
The True Way
  • To swapping happiness

37
Swap
  • 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

38
Now it works
  • We call it like this
  • int x 42
  • int y 84
  • swap(x, y)

39
Trace
  • 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
40
Trace
  • 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
41
Trace
  • 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
42
Trace
  • 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
43
Trace
  • 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
44
Trace
  • 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
45
Another 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

46
Questions?
47
Arrays
  • int ia6
  • Allocates consecutive spaces for 6 integers
  • How much space is allocated?

48
Arrays
  • 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
49
Arrays
  • 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
50
Arrays
  • int ia6
  • Note ia ? ia0
  • Never say, "Pointers and arrays are exactly the
    same thing!!!
  • int ip
  • ip ia / Okay /
  • ia ip / Illegal /

51
sizeof
  • 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

52
sizeof
  • int i
  • if sizeof(int) 4
  • then sizeof(i) 4

53
sizeof
  • 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!!!
54
sizeof
  • And...
  • int ia6
  • sizeof(ia) ? 24

55
Arrays
  • int ia6

Dont you ever use sizeof like this!!!
ia
  • ia4 means (ia 4)
  • which means (ia 4 sizeof(int))
  • Or (ia 4 sizeof(ia))

56
Pointer 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!

57
Pop Quiz
  • u16 shrtArr5 0, 1, 2, 3, 4
  • (shrtArr 2) 17
  • What does the array contain now?
  • Was this the same as
  • shrtArr2 17
  • ???

58
Arrays
  • int ia6

0
1
2
3
4
5
ia
  • Array elements are numbered like this since
    that's how the pointer arithmetic works out!

59
Arrays
  • int ia6

0
1
2
3
4
5
42
ia
ia4 42
60
Fun 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

61
Address 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!
62
Pop 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?
63
Pop 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?
64
More 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
65
Pointers to Pointers?
Name
Contents
Code int i
i
66
Pointers to Pointers?
Name
Contents
Code int i int ip
i
ip
67
Pointers to Pointers?
Name
Contents
Code int i int ip int ipp
i
ip
ipp
68
Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42
i
42
ip
ipp
69
Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip i
i
42
ip
ipp
70
Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip
i ip 84
i
84
ip
ipp
71
Pointers to Pointers?
Name
Contents
Code int i int ip int ipp i 42 ip
i ip 84 ipp ip
i
84
ip
ipp
72
Pointers 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
73
Hungarian 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

74
Don'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

75
More 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
76
More 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

78
Questions?
79
Arrays 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

80
Arrays 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
81
Arrays 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
82
Arrays 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
83
Arrays 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
84
Arrays 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
85
Questions?
86
Recall
  • int ia6
  • ia2 42

42
Address calculation 2 sizeof(ia) ia Access
is by dereferencing (2 sizeof(ia) ia)
Remember! You don't type in the sizeof part!
87
What happens?
  • int ia6
  • ia8 84

42
84
Address calculation 8 sizeof(ia) ia
Remember! You don't type in the sizeof part!
88
Questions?
  • Stop in the name of love!

89
Multi-Dimensional Arrays
  • How did multidimensional arrays work in Java?
  • More to the point...how do they work in Fortran?

90
Declaration
  • int ia34

Number of Columns
Number of Rows
Address
Type
Declaration at compile time i.e. size must be
known
91
How does a two dimensional array work?
How would you store it?
92
How would you store it?
93
Advantage
  • 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
94
What'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

?
95
What'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
  • 28
  • 20
  • error
  • 5
  • 7

?
96
What'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

?
97
Recall
  • 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

98
Element 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

99
Element 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)

100
What if array is stored in Column Major Order?
  • Element_Address
  • Array_Address
  • (Column_Index Num_Rows Row_Index)
  • Sizeof(Arr_Type)

101
Don'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
102
Multidimensional Example
  • void tester(int arr45)
  • int main()
  • int ia345
  • int ib845
  • int ic
  • ic ia
  • tester(ia)
  • tester(ib)
  • tester(ic)
  • return 0

103
What'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

104
Now think about
  • A 3D array

int a
105
Now think about
  • A 3D array

int a5
106
Now think about
  • A 3D array

int a45
107
Now think about
  • A 3D array

int a345
108
Offset to aijk?
  • A 3D array

int a345 slicesrowscolumns
offset (i rows columns) (j columns)
k
109
STOP
  • View the remainder of this presentation after the
    Dynamic Allocation presentation

110
Compare 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

111
6
112
5 x 6
113
4 x 5 x 6
114
3 x 4 x 5 x 6
115
Static Implementation
  • int arr3456
  • / initialization here /
  • someFunction(arr)
  • void someFunction(int array456)
  • / use array normally /
  • array1234 1234

116
One 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

117
Dynamic Implementation
  • Simple 2D Case

118
Pictorially
119
Pictorially
?
120
Pictorially
?
rows
121
Pictorially
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

124
arr23 17
cols
17
rows
125
Questions?
126
Question?
  • I understand clearly how to dynamically allocate
    a two dimensional array
  • Strongly agree
  • Agree
  • Neutral
  • Disagree
  • Strongly disagree

127
Dynamic Implementation
  • 4D Case

128
DynamicImplementation
  • 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
129
DynamicImplementation
  • void testfun(int a)
  • printf("d\n", a1234)
  • return

130
DynamicImplementation
  • int main()
  • int i, j, k, m
  • int pppparr
  • pppparr malloc(3sizeof(pppparr))

131
What's Going On?
int
pppparr
int
132
DynamicImplementation
  • int main()
  • int i, j, k, m
  • int pppparr
  • pppparr malloc(3sizeof(pppparr))
  • for(i0 ilt3 i)
  • pppparri malloc(4sizeof(pppparr))

133
What's Going On?
int
pppparr
int
int
134
DynamicImplementation
  • 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))

135
What's Going On?
int
pppparr
int
int
int
136
DynamicImplementation
  • 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))

137
What's Going On?
int
pppparr
int
int
int
int
138
DynamicImplementation
  • 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)

139
What's Going On?
int
pppparr
int
int
int
int
140
DynamicImplementation
  • void testfun(int a)
  • printf("d\n", a1234)
  • return

141
What's Going On?
a
int
pppparr
int
int
int
int
142
DynamicImplementation
  • void testfun(int a)
  • printf("d\n", a1234)
  • return

143
What's Going On?
int
pppparr
int
int
int
int
144
DynamicImplementation
  • void testfun(int a)
  • printf("d\n", a1234)
  • return

145
What's Going On?
int
pppparr
int
int
int
int
146
DynamicImplementation
  • void testfun(int a)
  • printf("d\n", a1234)
  • return

147
What's Going On?
int
pppparr
int
int
int
148
Summary
  • 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

149
Questions?
150
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com