## Lecture 21

p419-421, 422-430

### Additional notes on `new`

Consider the following example
`double *array;int size = 10;array = new double[size];`
The `new` operator does 2 things
• it allocates enough memory for the array. In the above code, 10 spaces are alloacted.
• it returns the address of the first element of the array.
When you should use dynamic arrays?
• you don't know the size (or max size) of the array when you write the program
• you want to save memory
One more simple example
 ```#includeusing namespace std;int main() { int size; int *squares; cout << "Enter an integer: "; cin >> size; squares = new int[size]; for(int i = 0; i < size; i++) { squares[i] = i*i; } return 0; } ```
What is the output of `square[3]` if `size` is 10?

One more example on returning an array
 ```#includeusing namespace std;// return an array with the first element is 1, the seconde element is 2 ....int* integers(int size); int main() { int size = 20; int* result; result = integers(size); for(int i = 0; i < 20; i++) { cout << result[i] << " "; } cout << endl; return 0;}int* integers(int size) { int *temp = new int[size]; // create a pointer to an array for(int i = 0; i < size; i++) { temp[i] = i+1; // assign value } return temp; // return the pointer(array) } ```

### Use pointer in functions

• use type* as a type
• Example, someFunction(int* a)

Example
 ```#includeusing namespace std;void increaseByOne(int* array, int size); void display(int* array, int size); int main() { int a[] = {2,4,6,8}; int size = 4; display(a,4); cout << endl; increaseByOne(a, 4); display(a,4); cout << endl; return 0; } void increaseByOne(int* array, int size) { for(int i = 0; i < size; i++) { array[i]++; } } void display(int* array, int size) { for(int i = 0; i < size; i++) { cout << array[i] << " "; } } ```

Some people like to write it in this way
 ```#includeusing namespace std;typedef int* IntPtr; void increaseByOne(IntPtr array, int size); void display(IntPtr array, int size); int main() { int a[] = {2,4,6,8}; int size = 4; display(a,4); cout << endl; increaseByOne(a, 4); display(a,4); cout << endl; return 0; } void increaseByOne(IntPtr array, int size) { for(int i = 0; i < size; i++) { array[i]++; }}void display(IntPtr array, int size) { for(int i = 0; i < size; i++) { cout << array[i] << " "; }}```

What is the output of the following program?
 ```#includeusing namespace std; void f1(double* a); void f2(double* a, double* b); void f3(double* a, double*& b); int main() { double *a, *b, x=10, y=20; a = &x; b = &y; cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f1(a); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f2(a,b); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; cout << endl; f3(a,b); cout << x << endl; cout << *a << endl; cout << y << endl; cout << *b << endl; return 0; } void f1(double* a) { *a = 7; } void f2(double* a, double* b) { *a = 55; b = a; } void f3(double* a, double*& b) { *a = 55; b = a; } ```
• pointers can be a call-by-value or a call-by-reference variable in functions
• You can't change the content of a call-by-value variable (i.e. you can't change what it points to) but you can change the content of the variable that it points to.
• You can change the content of a call-by-reference variable (i.e. you can change what it points to).
One more example
 ```#include#includeusing namespace std;// change the string to uppercase letterschar* changeToUpper(char* s); int main() { char string[] = "abcdefghijklmnopqrstuvwxyz1234567890"; char* upper; upper = changeToUpper(string); cout << string << endl << upper << endl; return 0; } char* changeToUpper(char* s) { int length = strlen(s); // the length of the string char* result = new char[length+1]; // dynamically created an array for(int i = 0; i < length; i++) { result[i] = toupper(s[i]); // return the uppercase letter of s[i] } result[length] = '\0'; // don't forget the null character return result; } ```

Output
 `abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`