Lecture 009

Pointer

int* pointer = alloc(int*);
*pointer = 0;

int**
pointer = alloc(int**);
*pointer = NULL;
*pointer = alloc(int**);
**pointer = 0;

// de-referencing *p //@requires p!=NULL; (you can only use NULL for pointers, not other data type)

alloc(p) //@ensures \result != NULL;

ptr->field //@requires ptr!=NULL; (checked by compiler)

Pointer vs. Array

Pointer vs. Array

Struct

Method: private Function: public

There is no method on c0 since it is not object oriented.

struct image_header {
  int width;      // field
  int height;     // field
  pixel_t[] data; // field
}; // remember to put this

// alias `struct image_header` to `image`
typedef struct image_header image;

int main() {
  // you have to use header* instead of header
  // because otherwise header will be too big
  // to store in local memory
  struct image_header* IMG = alloc(struct image_header);

  // using typedef
  image* IMG = alloc(struct image_header);

  //@assert IMG != NULL;

  // putting into field
  // IMG->width will be initialized to 0 initially.
  IMG->width = 3;

  // same as above, but meaningless in c0
  (*IMG).height = 2;

  // if data is not a valid field, compliller will give error
  //IMG->data is a 0 length array initially
  IMG->data = alloc_array(pixel_t, 2*3);


  return 0;
}

Libraries

Libraries

Libraries

// you can have a pointer to struct // but not the struct itself -> ssa_t is a pointer


struct ssa_header {
  string[] data;
  int length;
}

typedef struct ssa_header ssa;

int ssa_len(ssa_t A)
//@requires A != NULL;
//@ensures \result >= 0;

ssa_t ssa_new(int size)
//@requires 0 <= size;
//@ensures \result != NULL;
//@ensures ssa_len(\result) == size;

string ssa_get(ssa_t A, int i)
//@requires A != NULL;
//@requires 0 <= i && i < ssa_len(A);

void ssa_set(ssa_t A, int i, string x)
//@requires A != NULL;
//@requires 0 <= i && i < ssa_len(A);

typedef ssa* ssa_t;

Table of Content