CSE 303 Concepts and Tools for Software Development CSE 303 Lecture 9 10/16/2006 CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/16/2006 Lecture 9 – C Arrays and Strings
Administravia Homework #2 is due! Societal Implications Assignment #1 Due next lecture! 10/16/2006 CSE 303 Lecture 9
Tip of the Day Emacs Macros C-x ( : Start recording Macro All keystrokes recorded C-x ) : Stop recording Macro C-x e : run macro C-u <num> C-x e : run macro <num> times C-u runs any command multiple times 10/16/2006 CSE 303 Lecture 9
Last Time C-Program Structure Syntax Printf Declarations and Scope Argument Passing Syntax Booleans and arrays Confusing Cases Printf 10/16/2006 CSE 303 Lecture 9
Today Syntax Command-line Arguments Type names Arrays Strings 10/16/2006 CSE 303 Lecture 9
Type Names Can usually tell from declaration int i; : i has type (int) float f; : f has type (float) Pointers are a little more confusing int *p; : p has type (int*) or (int *) int **p; : p has type (int**) or (int **) int* p; : p has type (int*) or (int *) int* p,q; : q has type (int) We’ll see later when we discuss casting 10/16/2006 CSE 303 Lecture 9
Arrays in C Array : group of memory locations with Example: Same name Same type Example: int i; int c[3]; int j=23; for (i=0; i<3; i++) { c[i]=0; } Stack i 3 c[0] Increasing Addresses c[1] c[2] j 23 Note: Outside of the array, the order of variables is not defined. 10/16/2006 CSE 303 Lecture 9
Array Bounds Checking No array bounds check Not done by compiler Not done by run time system You must add checks or live without them Big source of errors 10/16/2006 CSE 303 Lecture 9
Pointer Arithmetic int i; int c[3]; int j=23; for (i=0; i<3; i++) { printf("%d\n", c[i]); printf("%d\n", *(c+i)); } Type of array name is pointer! Refers to address of first element in array Examples in simpleArray.c Stack i 3 c c[0] c+1 c[1] c+2 c[2] j 23 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 1 2 3 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 p 4 5 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 p 54 5 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 54 p 5 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 1 c[0] = 13; c[2] = 42; int *p = &c[4]; *p = 54; p++; *p = 64; Stack c[0] 13 1 42 3 54 p 64 6 7 8 10/16/2006 CSE 303 Lecture 9
Example 2 int i; for (i = 0; i <= 8; i++) { c[i] = c[i] + 10; } 1 2 3 4 5 6 7 8 int i; for (i = 0; i <= 8; i++) { c[i] = c[i] + 10; } c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9
Example 3 int i; for (i = 0; i <= 8; i++) { *(c+i) = *(c+i) + 10; } 1 2 3 4 5 6 7 8 int i; for (i = 0; i <= 8; i++) { *(c+i) = *(c+i) + 10; } c c+1 c+3 c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9
Example 4 int *p; for (p = c; p <= c+8; p++) { *p = *p + 10; } c[0] 1 2 3 4 5 6 7 8 int *p; for (p = c; p <= c+8; p++) { *p = *p + 10; } c c+8 c 10 11 12 13 14 15 16 17 18 10/16/2006 CSE 303 Lecture 9
Passing Arrays to Functions To pass array to function: Give name without brackets Common idiom: pass size in other arg Example: modify(c,size); Two function definition styles void modify(int c[], int size); void modify(int *c, int size); Examples in arrayArg.c 10/16/2006 CSE 303 Lecture 9
Strings Strings: Null terminator: no need to specify size Problems: An array of characters Terminated with "null character" Denoted with '\0' Character with ASCII value 0 Null terminator: no need to specify size Problems: Size of array must include space for '\0' Common bug: Overwrite '\0' 10/16/2006 CSE 303 Lecture 9
String Constants String Constants have type (char*) Automatically null terminated Example: char *str = "Hello"; str 'H' 'e' 'l' 'l' 'o' '\0' 10/16/2006 CSE 303 Lecture 9
string.h Various string utility functions Example See them with "man string" Also on p470 of Programming in C Example char s1[20] = "blue "; char s2[] = "gray"; char *s3 = strcat(s1, s2); 10/16/2006 CSE 303 Lecture 9
Arrays of Ponters Common idiom: combining multiple values char *s[3] = { "Hello", "World", "!" }; 'H' 'e' 'l' 'l' 'o' '\0' s[0] s[1] 'W' 'o' 'r' 'l' 'd' '\0' s[2] '!' '\0' 10/16/2006 CSE 303 Lecture 9
Command Line Arguments Main has other forms int main(int argc, char** argv); int main(int argc, char* argv[]); argc holds number of strings (first is cmd) argv holds strings Note use of common idioms array + length array of pointers Examples in commandLine.c 10/16/2006 CSE 303 Lecture 9
Summary Syntax Command-line Arguments Type names Arrays Strings 10/16/2006 CSE 303 Lecture 9
Reading Programming in C Chapter 7: Arrays Chapter 10: Strings pp259-271: Pointers and Arrays pp380-383: Command-Line Arguments 10/16/2006 CSE 303 Lecture 9
Next Time Structs The Heap Manual Memory Management 10/16/2006 CSE 303 Lecture 9