Lecture 22

Reading

p416, 429-430, 434

C-string and pointer

#include<iostream>
#include<cstring>
using 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: Charles
address:
1111 Westwood Blvd.
Los Angeles
CA 90024
gender: m
income: $123123
Compare the above program to this
struct Employee {
char name[50];
char address[100];
char gender;
double income;
}
Careful You should allocate memory to the pointers first before you use strlen or other functions.

Pointer arithmetic


Example
#include<iostream>
using namespace std;

int main() {
double *pt = new double;
cout << pt << endl;
cout << pt+1 << endl;
cout << pt+2 << endl;

return 0;
}

Output
002F10F0
002F10F8
002F1100
#include<iostream>
using 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;
}

delete operator


a very simple(and useless) example
#include<iostream>
using namespace std;

int main() {
int *temp, *temp2; temp = new int[10]; 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
#include<iostream>
using namespace std;

// returns sum of a[0], a[1], ....
int sumOfArray(int a[], int size);

// returns sum of 1 + 2 + 3 + .... n
int 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

#include<iostream>
using namespace std;
struct Stock {
char symbol[5]; // symbol, 4 characters
char name[26];
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
#include<iostream>
using namespace std;
struct Stock {
char symbol[5]; // symbol, 4 characters
char name[26];
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
#include<iostream>
using namespace std;
struct Stock {
char symbol[5]; // symbol, 4 characters
char name[26];
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;
}

copy

What is the output of the program and why?
#include<iostream>
using 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[5] = 'L';
wifename[6] = 'i';
wifename[7] = '\0';

cout << gfname << endl;
cout << wifename << endl;

return 0;
}