



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Optional Reading: Ford & Topp pp. 219-228; Koenig and Moo, Section 10.
float x = 15.5; float p; / equiv: float* p; */ p = &x; *p = 72; if ( x > 20 ) cout << "Bigger\n"; else cout << "Smaller\n";
The output is Bigger because x == 72.0. What’s going on? Computer memory
Before *p=
p
x
After *p=
p
15.5 x 72.
int * p, q; float *s, *t;
*p = 15;
float x=5, y=9; float *p = &x, *q = &y; *p = 17.0; *q = *p; q = p; *q = 13.0;
int *r; r = q; // Illegal: different pointer types; p = 35.1; // Illegal: float assigned to a pointer
int x = 10, y = 15; int *a = &x; cout << x << " " << y << endl; int *b = &y; *a = x * *b; cout << x << " " << y << endl; int *c = b; *c = 25; cout << x << " " << y << endl;
std::sort( a, a+n )
For each of the following problems, you may only use pointers and not subscripting:
cout << "Hello!" << endl;
string s1( "Hello!" ); string s2( h );
where h is as defined above.
int foo(int a, int *b) { int q = a+1; int r = *b+1; cout << "address of a = " << &a << endl; cout << "address of b = " << &b << endl; cout << "address of q = " << &q << endl; cout << "address of r = " << &r << endl; cout << "value at " << &a << " = " << a << endl; cout << "value at " << &b << " = " << b << endl; cout << "value at " << b << " = " << b << endl; cout << "value at " << &q << " = " << q << endl; cout << "value at " << &r << " = " << r << endl; return qr; }
int main() { int x = 5; int y = 7; int answer = foo (x, &y); cout << "address of x = " << &x << endl; cout << "address of y = " << &y << endl; cout << "address of answer = " << &answer << endl; cout << "value at " << &x << " = " << x << endl; cout << "value at " << &y << " = " << y << endl; cout << "value at " << &answer << " = " << answer << endl; }
address of a = 0x23eef address of b = 0x23eef address of q = 0x23eee address of r = 0x23eee value at 0x23eef0 = 5 value at 0x23eef4 = 0x23ef value at 0x23ef10 = 7 value at 0x23eee4 = 6 value at 0x23eee0 = 8 address of x = 0x23ef address of y = 0x23ef address of answer = 0x23ef0c value at 0x23ef14 = 5 value at 0x23ef10 = 7 value at 0x23ef0c = 48
answer=
0x23ef
48
0x23ef 0x23ef 0x23ef0c 0x23ef 0x23ef 0x23ef 0x23eefc 0x23eef 0x23eef 0x23eef 0x23eeec 0x23eee 0x23eee 0x23eee 0x23eedc 0x23eed
0x23ef 5 7
5
6 8
y=
x=
b= a=
q= r=