Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Lecture 3 Monday, 14 July 2003. Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.

Similar presentations


Presentation on theme: "C++ Lecture 3 Monday, 14 July 2003. Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l."— Presentation transcript:

1 C++ Lecture 3 Monday, 14 July 2003

2 Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l Relation between pointer and array l String, string processing functions

3 Arrays l Declaration of an array int c[12]; l or const int max = 12; int c[max]; l References to an array c[ ] c[0], c[1], …, c[max-1]

4 Array Initialization int n[5] = {1, 2, 3, 4, 5}; l or int n[ ] = {1,,2, 3, 4, 5}; l n[0], n[1], …, n[4] are valid references C.f. Fig. 4.3

5 Array of Characters (String) char string1[20]; char string2[ ] = "string literal"; l Individual array element is a character string2[3] = 'y'; C.f. Fig. 4.12

6 Passing Array l Passing an array name is equivalent to passing the address of the array int a[5]; // declare array func(a); // use function void func(int a[ ]) // define function { } C.f Fig. 4.14

7 Array Name is a constant Address int a[100]; a is the address of a[0], a+k is the address of a[k]. a–1, or a+100, is also a valid address; C/C++ cannot check array index out of bound error during compile or run time.

8 Multiple-Subscripted Arrays int a[2][5]; l A 2 by 5 array. l Initialization int a[2][5] = { {0,1,2,3,4}, {0,2,4,6,8} }; l Row major convention in C/C++

9 Argument Passing for Multi-Dimensional Array int a[3][5]; func(a); l or func(a+1); void func(int b[ ][5]) {... C.f Fig. 4.22

10 Pointers l Pointer value holds the address of a variable. l Pointers are also typed, i.e, pointer to int is considered different from pointer to double. l E.g., int *p; double *fpt;

11 Declaration of Pointer Variables int *p; // a pointer to int int c; // an int variable p = &c; // address of c is given to p l The value of variable p is the address of c. l The value at the address stored in p is the value of c. *p = 2; // dereferencing

12 Address Operator & int c, *p, a[100]; p = &c; // address of c p = a; // address of array p = &a[0]; // same as above

13 Dereferencing Operator * int *p, c; c = 1; p = &c; // let p point to c j = *p; // j gets 1,

14 Operator Precedence and Associativity ( ) [ ] left to right highest ++, --, &, * right to left unary * / % left to right multiply +, - left to right additive > left to right insertion, >= left to right relational ==, != left to right equality, left to right comma

15 Left to Right Association l a + b + c means (a+b) + c l Precedence l a + b*c means a + (b*c)

16 Adding Parentheses ( … ) to show the order of evaluation l b += x = y - k*a[ j ] && 3 | u < v--; l See Appendix A, page 1214 for the complete "Operator Precedence Chart".

17 Answer b += (x = ((y - (k*(a[ j ]))) && (3 | (u < (v--)))));

18 Call-by-Reference with Pointer l Passing a pointer can modify the value in calling program. int c; cube_it(&c); void cube_it(int *p) {... C.f. Fig.5.7

19 Pointer to const data and const Pointers void f(const int *p); l The data pointed by p cannot be modified. int * const p = &x; // p always // pointing to x const int *const p = &x; // p always // points to x, x cannot change

20 Relationship between Pointers and Arrays l An expression of *(a + k) l is considered totally equivalent to a[k]. l E.g., a[0] is the same as *a.

21 Multi-Dimensional Array l In the declaration int a[3][7]; we can view a as pointer to array of a[0], a[1], a[2], which themselves are pointers to int array a[i][j]. l **a is the same as a[0][0].

22 Pointer of Pointers a a[0] a[1] a[2] a[0][0] a[0][1] a[0][2]... a[1][0] a[1][1]... a[2][0] a[2][1]

23 Arrays of Pointers char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spade"}; l suit[0][0] refers to 'H', l suit[3][4] refers to 'e' in "Spade", l suit[2][20] is an illegal access.

24 Function Pointer void (*f)(int); l f is a pointer to a function of the form void g(int). l Function names are like address, e.g, f = g; (*f)(5); // call the function

25 Fundamentals of Characters and Strings char c, s[10]; // declare variables c = 'a'; // can be used as small int s = "John Q"; // don't work, why? l You must do s[0] = 'J'; s[1] = 'o'; s[2] = 'h'; // etc l Or use string copy function

26 String Initialization char color[ ] = "blue"; char *colorPtr = "blue"; char color[ ] = {'b', 'l', 'u', 'e', '\0'}; l What is the difference between the array color and the pointer colorPtr?

27 Read a String char word[20]; cin >> word; // cause problem cin >> setw(20) >> word; // OK l Read an entire line char sentence[80]; cin.getline(sentence, 80);

28 String Processing Functions char *strcpy(char *t, const char *s); char *strncpy(char *t, const char *s, size_t n); char *strcat(char *t, const char *s); int strcmp(const char *s, const char *t); size_t strlen(const char *s);

29 String Processing l Examples from Fig.5.30 and 5.31. C.f. Fig 5.30 and 5.31

30 Exercise 4.29 l (The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1. Use the sieve method to find prime. Initialize a[1000] all to 1 (for true). Starting from 2, 3, …, k,..., set array indices which are all multiple of k to 0, print the nonzero indices.


Download ppt "C++ Lecture 3 Monday, 14 July 2003. Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l."

Similar presentations


Ads by Google