Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 303 Concepts and Tools for Software Development

Similar presentations


Presentation on theme: "CSE 303 Concepts and Tools for Software Development"— Presentation transcript:

1 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

2 Administravia Homework #2 is due! Societal Implications Assignment #1
Due next lecture! 10/16/2006 CSE 303 Lecture 9

3 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

4 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

5 Today Syntax Command-line Arguments Type names Arrays Strings
10/16/2006 CSE 303 Lecture 9

6 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

7 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

8 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

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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 Summary Syntax Command-line Arguments Type names Arrays Strings
10/16/2006 CSE 303 Lecture 9

26 Reading Programming in C Chapter 7: Arrays Chapter 10: Strings
pp : Pointers and Arrays pp : Command-Line Arguments 10/16/2006 CSE 303 Lecture 9

27 Next Time Structs The Heap Manual Memory Management 10/16/2006
CSE 303 Lecture 9


Download ppt "CSE 303 Concepts and Tools for Software Development"

Similar presentations


Ads by Google