1
|
- Richard C. Davis
UW CSE – 10/16/2006
- Lecture 9 – C Arrays and Strings
|
2
|
- Homework #2 is due!
- Societal Implications Assignment #1
|
3
|
- 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
|
4
|
- C-Program Structure
- Declarations and Scope
- Argument Passing
- Syntax
- Booleans and arrays
- Confusing Cases
- Printf
|
5
|
- Syntax
- Type names
- Arrays
- Strings
- Command-line Arguments
|
6
|
- 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
|
7
|
- Array : group of memory locations with
- Example:
- int i;
- int c[3];
- int j=3D23;
- for (i=3D0; i<3; i++) {
- c[i]=3D0;
- }
|
8
|
- 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
|
9
|
- int i;
- int c[3];
- int j=3D23;
- for (i=3D0; 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
|
10
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
11
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
12
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
13
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
14
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
15
|
- c[0] =3D 13;
- c[2] =3D 42;
- int *p =3D &c[4];
- *p =3D 54;
- p++;
- *p =3D 64;
|
16
|
- int i;
- for (i =3D 0; i <=3D 8; i++) {
- c[i] =3D c[i] + 10;
- }
|
17
|
- int i;
- for (i =3D 0; i <=3D 8; i++) {
- *(c+i) =3D *(c+i) + 10;
- }
|
18
|
- int *p;
- for (p =3D c; p <=3D c+8; p++) {
- *p =3D *p + 10;
- }
|
19
|
- 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
|
20
|
- Strings:
- 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'
|
21
|
- String Constants have type (char*)
- Automatically null terminated
- Example: char *str =3D "Hello";
|
22
|
- Various string utility functions
- See them with "man string"
- Also on p470 of Progr=
amming
in C
- Example
- char s1[20] =3D "blue ";
- char s2[] =3D "gray";
- char *s3 =3D strcat(s1, s2);
|
23
|
- Common idiom: combining multiple values
- char *s[3] =3D { "Hello", "World", "!"=
};
|
24
|
- 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
|
25
|
- Syntax
- Type names
- Arrays
- Strings
- Command-line Arguments
|
26
|
- Programming in C
- Chapter 7: Arrays
- Chapter 10: Strings
- Chapter 11:
- pp259-271: Pointers and Arrays
- pp380-383: Command-Line Arguments
|
27
|
- Structs
- The Heap
- Manual Memory Management
|