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

30
C++ Lecture 3 Monday, 14 July 2003

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

Page 1: 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.

C++

Lecture 3Monday, 14 July 2003

Page 2: 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.

Arrays, Pointers, and Strings

Use of array in C++, multi-dimensional array, array argument passing

Pointers Relation between pointer and array String, string processing functions

Page 3: 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.

Arrays

Declaration of an arrayint c[12];

orconst int max = 12;int c[max];

References to an array c[ ]c[0], c[1], …, c[max-1]

Page 4: 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.

Array Initialization

int n[5] = {1, 2, 3, 4, 5}; or

int n[ ] = {1, ,2, 3, 4, 5};

n[0], n[1], …, n[4] are valid references

C.f. Fig. 4.3

Page 5: 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.

Array of Characters (String)

char string1[20];char string2[ ] = "string literal";

Individual array element is a character

string2[3] = 'y';C.f. Fig. 4.12

Page 6: 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.

Passing Array

Passing an array name is equivalent to passing the address of the array

int a[5]; // declare arrayfunc(a); // use functionvoid func(int a[ ]) // define function{} C.f Fig. 4.14

Page 7: 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.

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.

Page 8: 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.

Multiple-Subscripted Arrays

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

Page 9: 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.

Argument Passing for Multi-Dimensional Array

int a[3][5];func(a); or func(a+1);

void func(int b[ ][5]){ ... C.f Fig. 4.22

Page 10: 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.

Pointers

Pointer value holds the address of a variable.

Pointers are also typed, i.e, pointer to int is considered different from pointer to double.

E.g.,int *p;double *fpt;

Page 11: 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.

Declaration of Pointer Variables

int *p; // a pointer to intint c; // an int variablep = &c; // address of c is given to p The value of variable p is the address

of c. The value at the address stored in p is

the value of c.*p = 2; // dereferencing

Page 12: 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.

Address Operator &

int c, *p, a[100];

p = &c; // address of cp = a; // address of arrayp = &a[0]; // same as above

Page 13: 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.

Dereferencing Operator *

int *p, c;

c = 1;p = &c; // let p point to c

j = *p; // j gets 1,

Page 14: 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.

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

Page 15: 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.

Left to Right Association

a + b + c means (a+b) + c

Precedence

a + b*c means a + (b*c)

Page 16: 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.

Adding Parentheses ( … ) to show the order of

evaluation

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

See Appendix A, page 1214 for the complete "Operator Precedence Chart".

Page 17: 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.

Answer

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

Page 18: 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.

Call-by-Reference with Pointer

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

Page 19: 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.

Pointer to const data and const Pointers

void f(const int *p); The data pointed by p cannot be

modified.

int * const p = &x; // p always // pointing to xconst int *const p = &x; // p always // points to x, x cannot change

Page 20: 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.

Relationship between Pointers and Arrays

An expression of *(a + k)

is considered totally equivalent toa[k].

E.g., a[0] is the same as *a.

Page 21: 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.

Multi-Dimensional Array

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].

**a is the same as a[0][0].

Page 22: 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.

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]

Page 23: 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.

Arrays of Pointers

char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spade"};

suit[0][0] refers to 'H', suit[3][4] refers to 'e' in "Spade", suit[2][20] is an illegal access.

Page 24: 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.

Function Pointer

void (*f)(int); f is a pointer to a function of the form

void g(int).

Function names are like address, e.g, f = g;(*f)(5); // call the function

Page 25: 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.

Fundamentals of Characters and Strings

char c, s[10]; // declare variablesc = 'a'; // can be used as small ints = "John Q"; // don't work, why? You must do s[0] = 'J';s[1] = 'o';s[2] = 'h'; // etc Or use string copy function

Page 26: 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.

String Initialization

char color[ ] = "blue";char *colorPtr = "blue";char color[ ] = {'b', 'l', 'u', 'e', '\0'};

What is the difference between the array color and the pointer colorPtr?

Page 27: 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.

Read a String

char word[20];cin >> word; // cause problemcin >> setw(20) >> word; // OK

Read an entire linechar sentence[80];cin.getline(sentence, 80);

Page 28: 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.

String Processing Functions <string.h>

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);

Page 29: 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.

String Processing

Examples from Fig.5.30 and 5.31.

C.f. Fig 5.30 and 5.31

Page 30: 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.

Exercise 4.29

(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.