## Lecture 20

p176-178, p417-418, p422-427

### Pointer and array

• An array variable is actually the address of the first element of the array.
• An array variable is actually a pointer.
• Refer to p178, display 5.2
 #includeusing namespace std;int main() { int a[] = {1,2,3}; cout << "The address of a[0]: " << &a[0] <

Output on my computer
 The address of a[0]: 0012FF74a: 0012FF74

What is the output of the following program and why?
 #includeusing namespace std;int main() { int a[] = {11,22,33}; cout << *a << endl; //dereference return 0;}
 11
Reason: : a holds the address of a[0].

Can you guess what is the output of the following program?
 #includeusing namespace std;int main() { char ch[] = {'a','b','c'}; cout << *ch << endl << *(ch+1) << endl << *(ch+2) << endl; return 0; }
 a b c
Reason: The value of a is the address of a[0]. The value of a+1 is the address of a[1]. The value of a+2 is the address of a[2].

An array variable is actually a pointer.
What is the output of the following program?
 #includeusing namespace std; int main() { double x[2]; x[0] = 3.14; x[1] = 2.71; double *pt; pt = x; // assign x to pt cout << pt[0] << endl << pt[1] << endl; pt[0]++; cout << x[0] << endl << x[1] << endl; return 0;}
 3.14 2.71 4.14 2.71
One more example, what is the output of the following program?
 #include using namespace std; int main() { int *p; int a[10]; int b[10] = {2,4,6,8,10,12,14,16,18,20}; int index; // a[index] is index for(index = 0; index < 10; index++) a[index] = index; p = a; // p[index] is same as a[index] cout << "p array is :\n"; for(index = 0; index < 10; index++) { cout << p[index] << " "; } cout << endl; // change p[index] will change a[index] for(index = 0; index < 10; index++) { p[index] = p[index] + 1; } cout << "p array is :\n"; for(index = 0; index < 10; index++) { cout << p[index] << " "; } cout << endl; cout << "a array is: \n"; for(index = 0; index < 10; index++) { cout << a[index] << " "; } cout << endl; p = b; // p[index] is same as b[index] now cout << "p array is :\n"; for(index = 0; index < 10; index++) { cout << p[index] << " "; } cout << endl; return 0; }
 p array is : 0 1 2 3 4 5 6 7 8 9 p array is : 1 2 3 4 5 6 7 8 9 10 a array is: 1 2 3 4 5 6 7 8 9 10 p array is : 2 4 6 8 10 12 14 16 18 20

Trap: The following program can't be compiled, why?
 #includeusing namespace std; int main() { double x[2]; x[0] = 3.14; x[1] = 2.71; double *pt; x = pt; }
Error Message:
 Cannot convert from double * to double[2]
x is actually of type const double*. A constant can't be changed.

### typedef

• typedef is used to define an alias for any type name or definition.
• mainly used in defining type for pointer variables.
Example
 #includeusing namespace std; typedef int* IntPtr; int main() { IntPtr pt1, pt2; int x = 1, y = 2; pt1 = &x; pt2 = &y; cout << *pt1 << " " << *pt2 << endl; return 0; }

It is same as
 #includeusing namespace std; typedef int* IntPtr;int main() { int *pt1, *pt2; int x = 1, y = 2; pt1 = &x; pt2 = &y; cout << *pt1 << " " << *pt2 << endl; return 0; }
What is the advantage of using typedef?
Refer to the book p418
Avoid confusion. Example
 #include using namespace std; int main() { int *p1, p2; return 0; }
What is the type of p2 (an integer or a pointer) ?

### Dynamic arrays

What is wrong with the following program?
 #include using namespace std;int main() { int numOfStudents, index; cout << "Please enter the number of students: "; cin >> numOfStudents; double scores[numOfStudents]; cout << "Please enter the scores:\n"; for(index = 0; index < numOfStudents; index++) { cin >> scores[index]; } cout << "The scores are:\n"; for(index = 0; index < numOfStudents; index++) { cout << scores[index] << " "; } return 0; }

When you declare an array, you must estimate the largest possible size you may need for the array.
What is the disadvantage of this?
• You underestimate the size. Your array is not big enough to hold all the data.
• You overestimate the size, as a result, you waste some computer memory.
Dynamically allocated array can be used to solve this problem.
The above program can be rewritten as
 #include using namespace std;int main() { int numOfStudents, index; double* scores; cout << "Please enter the number of students: "; cin >> numOfStudents; scores = new double[numOfStudents]; // dynamically allocated array cout << "Please enter the scores:\n"; for(index = 0; index < numOfStudents; index++) { cin >> scores[index]; } cout << "The scores are:\n"; for(index = 0; index < numOfStudents; index++) { cout << scores[index] << " "; } cout << endl; return 0;}

Output
 Please enter the number of students: 5 Please enter the scores: 90.5 91.25 55 25.9 73 The scores are: 90.5 91.25 55 25.9 73
• scores = new double[numOfStudents]: The size of the dynamic array (numOfStudents) is given in square bracket.
• The size can be given using an int variable or other expression.
• The size can be a variable of type int whose value is determined while the program is running.
• It is used like ordinary array.

### Return an array

• Recall that you can't return an array in a function
• instead you should return a pointer
int[] someFunction(); // illegal!!!!
int* someFunction(); // legal!
Example:
 #include using namespace std;int* doubler(int a[], int size); int main() { int a[] = {1,2,3,4,5}; int *b; b = doubler(a,5); int i; // display a cout << "Array a:\n"; for(i = 0; i < 5; i++) { cout << a[i] << " "; } cout << endl; // display b cout << "Array b:\n"; for(i = 0; i < 5; i++) { cout << b[i] << " "; } cout << endl; return 0; } int* doubler(int a[], int size) { int* temp = new int[size]; for(int i = 0; i < size; i++) { temp[i] = 2*a[i]; } return temp; }

Output
 Array a:1 2 3 4 5Array b:2 4 6 8 10