Changes between Version 7 and Version 8 of Arrays


Ignore:
Timestamp:
09/15/13 09:51:46 (13 years ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Arrays

    v7 v8  
    1 == First-class Arrays ==
     1= First-class Arrays =
     2
     3== Motivation and basic concepts ==
    24
    35C uses an "array-pointer" pun.  This is not a particularly useful feature and leads to a lot of confusion.  In C, code such as
     
    5153might 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.
    5254
    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
     57You can still modify first-class arrays and create references to elements of them, just as with regular arrays.  Example:
     58
    5459{{{
    5560void f(int a[[]]) {
     
    6772  g(&a[0]);
    6873  // now a = {10}
     74}
     75}}}
     76
     77Another example:
     78{{{
     79void 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
    6995}
    7096}}}