Class Notes (836,564)
Canada (509,855)
CS 137 (16)
Lecture 16

Programming Lecture 16.docx

4 Pages
Unlock Document

Computer Science
CS 137
Andrew Morton

Programming Lecture 16 November 13, 2012 Switch Statement  switch (expression) { case constant.expression: statement // end with “break;” case constant.expression: statement // end with “break;” … default: statements } Example #include void standing (char grade) { switch (grade) { case ‘A’: case ‘a’: printf(“Excellent\n”); break; case ‘B’: case ‘b’: printf (“Good\n”); break; case ‘C’: case ‘c’: printf(“Satisfactory\n”); break; case ‘D’: case ‘d’: printf(“Poor\n”); break; case ‘E’: case ‘e’: printf(“Fail\n”); break; default: printf(“Invalid grade\n”); } } Dynamic Storage  #include , malloc, free  void *realloc (void *p, size _t size); o Reallocates previously allocated block of memory (usually to make bigger) o Pointer may change o Can grow by appending storage or allocating new storage and copying contents Example Safe.h void *safeMalloc (size_t size); void *safeRealloc (void *p, size_t size); // Repackage malloc and realloc to check for failure Safe.c #include “safe.h” #include void *safeMalloc (size_t size) { void *p = malloc(size); assert (p); return (p); } Pointers to structs example struct tod {int hour, minute;}; struct tod *t = (struct tod *) malloc (sizeof(struct tod)); // accessing members are (*t).hour = 18; or t->hour = 18  Better arrays o Grow a
More Less

Related notes for CS 137

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.