Download presentation
Presentation is loading. Please wait.
Published byToby James Modified over 8 years ago
1
Announcements Quiz this Thursday 1
2
Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char board[10][10]; foo(board); } Why? An array is the same a pointer, right? Isn’t a two dimensional array a pointer to pointer? 2
3
Example char ** (accessing with [][]) 3 Declared as char ** Accessed using [i][j]
4
Example char ** (accessing with [][]) 4
5
declaring as [][] accessing [][] 5 Declared as a[ ] [ ] Accessed as a[ ] [ ]
6
declaring as [][] accessing [][] 6
7
declaring [i][j] calling ** 7 Declared as a[ ] [ ] called as **
8
declaring [i][j] calling ** 8
9
9 What’s going on??
10
Array Declaring char a[25]; allocates consecutive memory for 25 chars and points a to it. Declaring char a[3][25]; allocates consecutive memory for 75 chars and points a to it. The memory is organized row by row. 10 row 0 row 1 row 2 a 0, 1,..., 24
11
Array char a[25]; a[3] (same as *(a + 3)) is the start location + 3, or the 4 th element. char a[3][25]; a[0][2] (same as *(a + 2)) is the third item of the first (index 0) row. What is address of third item of third row? 11 row 0 row 1 row 2 a 0, 1,..., 24
12
Array char a[3][25]; a[0][2] (same as *(a + 2)) is the third item of the first (index 0) row. What is address of third item of third row? a[2][2] (same as *(a + 50 + 2) or *(a + 52) ) 12 row 0 row 1 row 2 a 0, 1,..., 24
13
Array char a[3][25]; What is address of third item of third row? a[2][2] (same as *(a + 50 + 2) or *(a + 52) ) Note we get this by adding the size of the row times the number of the rows we’re skipping over. (in this case 25 times 2) 13 row 0 row 1 row 2 a 0, 1,..., 24
14
Why the call is a problem Why the call is a problem: int foo(char **a) { } int main() { char board[10][10]; foo(board); How can foo know how to interpret a[2][4] if it doesn’t know the size of the rows? 14
15
Correct Call 15 Notice the size of the row is passed.
16
Structs A struct is – an aggregate data structure, i.e., a collection of other data; – can contain components (“fields”) of different types by contrast, arrays contain components of the same type – fields are accessed by name by contrast, array elements are accessed by position Unlike Java classes, a struct can only contain data, not code. 16
17
Declaring structs A node for a linked list of integers: struct node { int val; struct node *next; } 17 optional “structure tag” – used to refer to the structure struct node val next
18
Accessing structure fields Given – a struct s containing a field f to access f, we write s.f Example: struct foo { int count, bar[10]; } x, y; x.count = y.bar[3]; Given – a pointer p to a struct s containing a field f to access f we write p->f // eqvt. to: (*p).f Example: struct foo { int count, bar[10]; } *p, *q; p->count = q->bar[3]; 18 declares x, y to be variables of type “struct foo”
19
Example: sorting a linked list of strings 19 struct s str next points to a string points to another list node declares list_hd as “pointer to something of type struct s”
20
Example: sorting a linked list of strings 20 allocate memory for a list node amount allocated = size of the struct (not the pointer to the struct)
21
Example: sorting a linked list of strings 21 fill in the fields of the newly allocated struct add it to the head of the linked list tmpnode, buf will get deallocated does this cause any problems?
22
Example: sorting a linked list of strings 22 traverse the list compare strings by lexicographic ordering idiomatic C: “iterate as long as ptr NULL
23
Example: sorting a linked list of strings 23 input strings sorted output
24
Operator Precedence and Associativity 24 Operator precedence and associativity define how an expression is parsed and evaluated – The text (King, C Programming: A Modern Approach), Appendix A has a full list of all C operator precedences Some highlights: in decreasing order of precedence: – postfix expressions ( [ ] ( ) ->. ++ postfix -- postfix ) – unary expressions ( ++ prefix -- prefix & * + - ~ ! sizeof ) – type cast – arithmetic: multiplicative additive bit-shift – relational (not all of the same precedence) – bitwise operators (not all of the same precedence)
25
Operator Precedence Examples Decreasing order of precedence: – postfix expressions [ ] ( ) ->. ++ post -- post – unary expressions ++ pre -- pre & * deref + - ~ ! sizeof – type cast – arithmetic – … How are these parsed? *p++ *p->q *A[10] *p->q++ 25 ++ binds tighter than *: *(p++) not: (*p)++ -> binds tighter than *: *(p->q) not: (*p)->q [ ] binds tighter than *: *(A[10]) not: (*A)[10] -> and ++ left-associative: *( (p->q) ++ )
26
Bit Operations C provides operations to manipulate individual bits of values – done properly and in the right places, this can lead to elegant and efficient code – no. of bits per byte may vary across architectures: the macro CHAR_BIT (defined via #include ) gives the no. of bits in a byte. 26
27
Bit Operations OperationOperator Bitwise complement (unary) ~ Bitwise shift >> << Bitwise AND & Bitwise XOR ^ Bitwise OR | 27 precedence high low
28
Bit Masks We can select (look at, define) specific bits of a value using bit masks (a fixed bit pattern). E.g.: – for least significant bit in a byte: 0x1 – for most significant bit in a byte: (0x1 << (CHAR_BIT–1)) More generally: let MASK = 0 … 0 i +1 1 i 0 i -1 … 0 0 then, for a value x: only bit i of x: (x & MASK) x with bit i set to 1: (x | MASK) x with bit i set to 0: (x & ~MASK) 28 i
29
Example Use: Representing Fixed-size Sets Intuition: – each distinct element that can be in the set is assigned a bit position – suppose x is a variable that holds such a set, and p is an element whose assigned position is k. Then: to add p to x: x = x | (1 << k) to check whether x is in p: evaluate x & (1 << k) set union: bitwise or, | set intersection: bitwise and, & set complement: bitwise negation, ~ But what if the size of the set is too big for an int? 29
30
Representing Fixed-size Sets To handle sets that may be too large to represent using a single word (e.g., int): – each element still gets assigned a fixed position k (now k may be larger than 32 or 64) – use an array of ints (or longs) to represent the set – use a 2-level mapping to map the position k to a bit position in this array: first figure out which array element we need then figure out which bit position in this array we need 30 1
31
Representing Fixed-Size Sets: Example Suppose we have: – a set that can contain upto 350 elements – an int holds 32 bits – an element p corresponds to position 212 in the set Then: – use an array of 11 ints to represent the set (11 x 32 = 352) – p is in element 7 of this array (e.g., A[7]) (element 7 holds positions 192–223) – within element 7 of the array, p is at bit position 20. 31
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.