


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Koenig and Moo, Section 10.1; Malik, pages 742-
float x = 15.5; 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?
p = &x;
Takes the address of x’s memory location and stores it (the address) in the memory location associated with p.
*p = 72;
causes the computer to get the memory location stored at p, then go to that memory location, and store 72 there. This writes the 72 in x’s location.
16.4 Defining Pointer Variables
int * p, q; float *s, *t;
*p = 15;
16.5 Operations on Pointers
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
16.6 Exercise
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;
16.10 Exercises
For each of the following problems, you may only use pointers (array iterators) and not subscripting:
16.11 Character Arrays and String Literals
cout << "Hello!" << endl;
"Hello!" is a string literal. It is also an array of characters (with no associate variable name).
char h[] = {’H’, ’e’, ’l’, ’l’, ’o’, ’!’, ’\0’};
or as:
char h[] = "Hello!";
In either case, array h has 7 characters, the last one being the null character.
16.12 Conversion Between Standard Strings and C-Style String Literals
string s1( "Hello!" ); string s2( h );
where h is as defined above.