Changes between Version 7 and Version 8 of Arrays
- Timestamp:
- 09/15/13 09:51:46 (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Arrays
v7 v8 1 == First-class Arrays == 1 = First-class Arrays = 2 3 == Motivation and basic concepts == 2 4 3 5 C uses an "array-pointer" pun. This is not a particularly useful feature and leads to a lot of confusion. In C, code such as … … 51 53 might be the declaration of a function that takes an array of messages and returns an array one longer which is equivalent to the original array with `m` added. There is no "sharing" between these two arrays and no array is modified. 52 54 53 You can still modify first-class arrays and create references to elements of them, just as with regular arrays: 55 == Modifying first-class arrays == 56 57 You can still modify first-class arrays and create references to elements of them, just as with regular arrays. Example: 58 54 59 {{{ 55 60 void f(int a[[]]) { … … 67 72 g(&a[0]); 68 73 // now a = {10} 74 } 75 }}} 76 77 Another example: 78 {{{ 79 void f() { 80 int a[[3]]; 81 int b[[2]]; 82 83 a[0]=0; a[1]=1; a[2]=2; 84 b[0]=9; b[1]=99; 85 86 int *p = &a[1]; 87 int *q = &a[2] 88 89 // p = "pointer to element 1 of a", and *p evaluates to 1 90 // q = "pointer to element 2 of a", and *q evaluates to 2 91 a = b; 92 // now a = {9, 99} 93 // p is still "pointer to element 1 of a", and *p evaluates to 99 94 // q is still "pointer to element 2 of a", and evaluating *q yields a runtime array index out of bounds error 69 95 } 70 96 }}}
