{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs246-s11-001-may26

# cs246-s11-001-may26 - CS 246(Spring 2011 Section 1 Thursday...

This preview shows pages 1–7. Sign up to view the full content.

CS 246 (Spring 2011) – Section 1 Thursday 26 May Pointers/References, Arrays, Structures

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 26 May 2 Today's Agenda Some notes about last class Finish 2.7.2 (pointers/references) 2.7.3 Aggregates Arrays Structures
CS 246 (Spring 2011) - Section 001 - Thursday 26 May 3 Continued from last day Partial memory diagram int x = 5; int *p1; Rewrite rules for p1 = &x; Can a pointer variable point to itself?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 26 May 4 Reference pointers Some motivation Say p1 and p2 are of type int * *p1 = (*p1-*p2)/(*p2 * *p1)+(10/*p1); (tedious and error-prone) p1 = (p1 – p2) / (p2 * p1) + (10 / p1); (would be much nicer) Reference pointers provide implicit dereference int z = 3, u = 4; int &r1 = z; int &r2 = u; cout << r1 + r2 << endl;
CS 246 (Spring 2011) - Section 001 - Thursday 26 May 5 Reference pointers (ctd.) The name of a reference pointer means the value of its target r1 = r2; // rewrite rules? cout << r1 << endl; u = 20; cout << r1 << endl; The address of a reference pointer means the address of its target cout << z << “ “ << r1 << endl; cout << &z << “ “ << &r1 << endl;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 26 May 6 Reference pointers (ctd.) Reference pointers are treated like a const (literal) Its contents cannot change at run-time Must be initialized when declared
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}