## Lecture 22

p416, 429-430, 434

### C-string and pointer

• A c-string is an array of characters and thus is a pointer of char type.
 ```#include#includeusing namespace std;struct Employee { char *name; char *address; char gender; double income; }; void display(Employee employee); int main() { Employee charles; char myname[] = "Charles"; charles.name = new char[strlen(myname)+1]; strcpy(charles.name, "Charles"); char myaddress[] = "1111 Westwood Blvd.\nLos Angeles\nCA 90024"; charles.address = new char[strlen(myaddress)+1]; strcpy(charles.address, "1111 Westwood Blvd.\nLos Angeles\nCA 90024"); charles.gender = 'm'; charles.income = 123123.25; display(charles); return 0; } void display(Employee employee) { cout << "name: " << employee.name << endl << "address: "<< endl << employee.address << endl << "gender: " << employee.gender << endl << "income: \$" << employee.income << endl; } ```

Output
 `name: Charlesaddress:1111 Westwood Blvd.Los AngelesCA 90024gender: mincome: \$123123`
Compare the above program to this
`struct Employee {   char name;   char address;   char gender;   double income;}`
Careful You should allocate memory to the pointers first before you use `strlen` or other functions.

### Pointer arithmetic

• you can perform arithmetic on pointers
• it is the arithmetic on address (not the arithmeic on numbers, or on what it points to)
• You can do addition or subtraction (multiplication and division are meaningless)

Example
 `#includeusing namespace std;int main() { double *pt = new double; cout << pt << endl; cout << pt+1 << endl; cout << pt+2 << endl; return 0;}`

Output
 `002F10F0002F10F8002F1100`
• the address is represented by a hexadecimal number
• `pt+1` does not really add `1` to `pt`. It actually add `8` to `pt`. The increment is related to the size of the type. Try to replace the `double` by `int`.
• `pt+1, pt+2` are actually pointing to something meaningless.
 `#includeusing namespace std;int main() { int a[] = {2,3,5,7,11}; int i; for(i = 0; i < 5; i++) { cout << a[i] << " "; } cout << endl; for(i = 0; i < 5; i++) { cout << *(a+i) << " "; } cout << endl; return 0;}`
• Most of the time, we use pointer arithmetic on array
• `a + i` points to `a[i]`. i.e. `*(a+i)` is `a[i]`

### `delete` operator

• free the memory that used by a pointer
• may not be useful in PIC10A but will be useful in PIC10B and PIC10C

a very simple(and useless) example
 ```#includeusing namespace std;int main() { int *temp, *temp2; temp = new int; temp2 = new int; for(int i = 0; i < 10; i++) { *(temp + i) = i; } delete [] temp; // release the memory occpied by temp delete temp2; return 0; } ```

a better example
 ```#includeusing namespace std;// returns sum of a, a, ....int sumOfArray(int a[], int size);// returns sum of 1 + 2 + 3 + .... nint sum(int n);int main() { cout << sum(100) << endl; // 5050 return 0;}int sumOfArray(int a[], int size) { int sum = 0; for(int i = 0; i < size; i++) { sum+=a[i]; } return sum;}int sum(int n) { int* temp = new int[n]; for(int i = 0; i < n; i++) { temp[i] = i+1; } int result = sumOfArray(temp, n); delete [] temp; // don't need temp any more return result; } ```

### Struct and pointer

• Most of the examples in my handout use `int, double, char` pointers
• you can use pointer for structure.
 ```#includeusing namespace std;struct Stock { char symbol; // symbol, 4 characters char name; double price;};int main() { Stock yahoo = {"yhoo", "Yahoo Inc", 10.5}; Stock *pt = &yahoo; cout << "The symbol of " << (*pt).name << " is " << (*pt).symbol << endl; return 0;}```

It is same as
 ```#includeusing namespace std;struct Stock { char symbol; // symbol, 4 characters char name; double price;};int main() { Stock yahoo = {"yhoo", "Yahoo Inc", 10.5}; cout << "The symbol of " << yahoo.name << " is " << yahoo.symbol << endl; return 0;} ```

Here is the output
 `The symbol of Yahoo Inc is yhoo`

-> operator
Another way to write the program is
 ```#includeusing namespace std;struct Stock { char symbol; // symbol, 4 characters char name; double price;};int main() { Stock yahoo = {"yhoo", "Yahoo Inc", 10.5}; Stock *pt = &yahoo; cout << "The symbol of " << pt->name << " is " << pt->symbol << endl; return 0;}```
• `pt->name` is same as `(*pt).name`
• the first notation is more convenient and is almost always that notation used.

### copy

What is the output of the program and why?
 `#includeusing namespace std;int main() { char gfname[] = "Mary Smith"; char* wifename; wifename = gfname; // copy girlfriend's name // change my wife's last name to Li wifename = 'L'; wifename = 'i'; wifename = '\0'; cout << gfname << endl; cout << wifename << endl; return 0;}`