=============================== WEEK 2 ======================================== I. Function Prototypes - lists function name, return type and parameters - so compiler can do type checking - it is just the declaration of a procedure w/ a semicolon. - important for large programs w/ multiple files - C without prototypes - can use different number of arguments vs. parameters - can omit return value - C uses arbitrary values if not defined II. Variable initialization - local variables/arrays initially have an arbitrary value - must initialize all local variables! III. Basic memory and pointers - computers work in binary (base 2) ex: 101 base 2 = 5 base 10 - bits - the smallest unit of memory, can be either "0" or "1" - bytes - group bits together into a "word" - 8 bits forms 1 byte - size of bits indicates the number of "digits" available for the number your wish to store - the more bits/bytes, the larger the number that can be stored - char = 1 byte, short = 2 bytes, int = 4 bytes - number of values that can be stored is 2^(number-bits) - signed vs. unsigned (do we use one bit as a sign bit?) - char can store -128 to 127 - unsigned char can store 0 to 255 - sizeof() - returns bytesize of a data structure - may be padded for alignment - addresses of memory - each memory location has an "address" associated with it - currently a 4-byte number - &x returns the address of variable x - can store the address as an int - the pointer - like a Java reference, but pointer can point to anything (simply an address to a memory location) - type of pointer lets C compiler know how to deference - declarations (int *p) ex: int *p = &x; - accessing the contents of the pointer: p - accessing the contents of the memory location pointed to by the pointer: *p - pointers and arrays - the array name is really a pointer to the first element of the array int a[10]; int p*; p = a; or p = &(a[10]); - when passing an array as a parameter - are only passing a pointer to the array - pointer arithmetic (p++) - increments p by the size of the data type p references ex: char string[6] = "hello"; char *ptr = string; *ptr == 'h' *(ptr + 1) = 'e'; *(ptr + 2) = 'l'; what about *ptr + 3? - Example: write strcpy: char string1[80], string2[80]; strcpy(string1, string2); void strcpy(char *to, char *from) { int i; while (from[i] != '\0') { to[i] = from[i]; i++; } to[i] = '\0'; } - in C, every expression returns a value. - for "=", value returned is value set void strcpy(char *to, char *from) { int i; while ((to[i] = from[i]) != '\0') i++; } - if we use pointers, we can remove i void strcpy(char *to, char *from) { while ((*to = *from) != '\0') to++; from++; } - i++ increments i after using value in command ++i increments i before using value in command (value of i++ is value of i before it is incremented) - ++ and * are evaluated right to left, so *i++ equiv to *(i++) void strcpy(char *to, char *from) { while ((*to++ = *from++) != '\0') ; } - a last version: void strcpy(char *to, char *from) { while (*to++ = *from++) ; }