Professional Documents
Culture Documents
Review: More About Pointers Pointers To Pointers Pointer Arrays Multidimensional Arrays
Review: More About Pointers Pointers To Pointers Pointer Arrays Multidimensional Arrays
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
alignment
anonymous or named
} u ;
unsigned i n t f l a g _ s : 1 ;
unsigned i n t f l a g _ u : 2 ;
unsigned i n t bar ;
};
2 bytes
4 bytes,
4 byte-aligned
bit fields
4 bytes,
4 byte-aligned
i n t i ;
char c ;
} u ;
unsigned i n t bar ;
short s ;
unsigned i n t f l a g _ s : 1 ;
unsigned i n t f l a g _ u : 2 ;
};
sizeof(struct foo)
= 12
added/removed
Node in list or tree usually implemented as a struct
Use malloc(), free(), etc. to allocate/free memory
dynamically
Unlike arrays, do not provide fast random access by index
(need to iterate)
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
Pointer review
pointer to int
pointer to structure div_t
double p p i = & p i ;
p r i n t f ( "pi = %g\n" , p p i ) ;
multidimensional arrays
Pointers to pointers
that pointer
i n t n = 3 ;
i n t pn = &n ; / p o i n t e r t o n /
i n t temp = a ;
a = b ;
b = temp ;
i n t temp = a ;
a = b ;
b = temp ;
Pointer arrays
arr
Can declare a pointer array int sorted_array[100]; containing
Insertion sort:
/ move p r e v i o u s elements down u n t i l
i n s e r t i o n p o i n t reached /
void s h i f t _ e l e m e n t ( unsigned i n t i ) {
i n t pvalue ;
/ guard a g a i n s t going o u t s i d e a r r a y /
f o r ( pvalue = s o r t e d _ a r r a y [ i ] ; i &&
s o r t e d _ a r r a y [ i 1] > pvalue ; i ) {
/ move p o i n t e r down /
s o r t e d _ a r r a y [ i ] = s o r t e d _ a r r a y [ i 1];
}
s o r t e d _ a r r a y [ i ] = pvalue ; / i n s e r t p o i n t e r /
}
10
11
String arrays
chars
Each string may be of different length
char
char
char
char
s t r 1 [ ] = "hello" ; / l e n g t h = 6 /
s t r 2 [ ] = "goodbye" ; / l e n g t h = 8 /
s t r 3 [ ] = "ciao" ; / l e n g t h = 5 /
s t r A r r a y [ ] = { s t r 1 , s t r 2 , s t r 3 } ;
themselves!
12
Multidimensional arrays
brackets notation:
int world [20][30]; is a 20x30 2-D array of ints
Higher dimensions possible:
char bigcharmatrix [15][7][35][4];
this?
Multidimensional arrays are rectangular; pointer arrays can
be arbitrary shaped
13
Review
More about Pointers
Pointers to Pointers
Pointer Arrays
Multidimensional Arrays
Data Structures
Stacks
Queues
Application: Calculator
14
14
The stack
15
Stack as array
allocation):
int stack_buffer [100];
track end:
int itop = 0; / end at zero => initialized for empty stack /
16
Stack as array
s t a c k _ b u f f e r [ i t o p ++] = elem ;
i f ( i t o p > 0 )
r e t u r n s t a c k _ b u f f e r [ i t o p ] ;
else
r e t u r n 0 ; / o r o t h e r s p e c i a l v a l u e /
17
i n t element ;
s t r u c t s _ l i s t n o d e pnext ;
};
struct s_listnode stack_buffer = NULL;
start empty
18
19
r e t u r n 0 ; / o r o t h e r s p e c i a l v a l u e /
20
The queue
Rx packet queues)
Imposes an ordering on elements
21
Queue as array
(front)
Need to keep track of front and rear:
int ifront = 0, irear = 0;
elements:
int ifront = 0, icount = 0;
22
Queue as array
i c o u n t ;
r e t u r n q u e ue _ b uf f e r [ i f r o n t + + ] ;
} else
return 0 . ; / or other s p e c i a l value /
}
23
Queue as array
of capacity 4:
a
rear
front
front
rear
24
Queue as array
Dequeue a:
front
rear
25
Queue as array
26
Queue as array
this harder?
27
f l o a t element ;
s t r u c t s _ l i s t n o d e pnext ;
};
struct s_listnode queue_buffer = NULL;
start empty
28
p r e a r = new_node ;
not empty
29
r e t u r n 0 . ; / o r o t h e r s p e c i a l v a l u e /
queue is empty
30
A simple calculator
evaluator
Prex, inx, postx notation: operator before, between, and
Inx
Prex
Postx
A + B
+AB
A B +
-*ABC
A * B - C
A B * C
( A + B ) * ( C - D)
* + A B - C D A B + C D - *
31
Inx to postx
32
Inx expression: A + B * C - D
Token
Output queue
Operator stack
A
A
+
+
A
+
AB
B
+*
AB
*
+*
ABC
C
ABC*+
ABC*+D
D
(end) A B C * + D Postx expression: A B C * + D
What if expression includes parentheses?
33
Inx expression: ( A + B ) * ( C - D )
Token
Output queue
Operator stack
(
(
A
A
(
+
A
(+
B
AB
(+
AB+
)
AB+
*
*
AB+
(
*(
AB+C
C
*(
AB+C
*(
AB+CD
D
*(
AB+CD
*
)
(end) A B + C D - *
Postx expression: A B + C D - *
34
Evaluating postx
35
Postx expression: 3 4 + 5 1 - *
Token
Stack
3
4
34
+
7
5
75
1
751
74
*
28
(end)
answer = 28
Extends to expressions with functions, unary operators
Performs evaluation in one pass, unlike with prex notation
36
Summary
Topics covered:
Pointers to pointers
pointer and string arrays
multidimensional arrays
Data structures
37
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.