Discussion slides for weThe UniversiLast Updated: Septemeek of September 10, 2007ty of Michiganmber 12, 2007 3:00PM1Outline● Pointers / The ternary (conFile I/O●File I/O● Make utility● GDB● gProf:ditional) operator Questions?2Pointers● A pointer is a variable that data object in memory (or ● Declare pointers with the *int* x;int *x;char* c;;char *c;Question: what is the differ●Question: what is the differstar with the type, and withAnswer: the two are equiva●Answer: the two are equivaseparate the * from both: i: contains an address of some sometimes, a function).* (star) operator. Examples:rence between placing the rence between placing the h the variable?alent We could also alent. We could also int * x is perfectly fine.3Pointers - initialization● We've declared but not defithey're pointing at whatevesome random memory loca● We should initialize our poidon't yet have anything, it'with NULL, (0). NULL meanpointing to anything.” pointing to anything. int* x = NULL;char* c = NULL;:fined them. Right now, er happened to be there; i.e. ation. This is dangerous.inters to something. If we 's good practice to initialize ns “this pointer isn't (yet) 4Pointers – Address-of opera● We need a way to retrieve (e.g. variables). Use the adby &.int myInt=42;int myInt 42;int* pMyInt = &myInt;● The situation is a little bit dalready denote addresses, int myIntArray[ 5 ] = { 2, int* pMyIntArray = myIntArr:atorthe address of data objects ddress-of operator, denoted different for arrays. Arrays so no & is needed: 4, 6, 8, 10 };ray;5Pointers – Dereferencing● Now we need a way of obtaaddress. This is known as dereferencing operator. Yothe inverse operator of &.● WARNING: There may be pointer declaration, and theare separate things, and caare separate things, and cacontexts.int myInt = 42;int myInt = 42;int* pMyInt = &myInt;cout << *pMyInt << endl;( *pMyInt ) *= 2;t<<*MIt<< dlcout << *pMyInt << endl;cout << myInt << endl;:aining the data from a given dereferencing. * is the ou can think of * as kind of confusion between the * for e * for dereferencing. These an be differentiated by their an be differentiated by their 6Pointers – Dereferencing● What happens if we do somcode fragment?int myInt = 42;( *myInt ) *= 2;:mething like the following 7Pointers – Example 1● What will the following functionvoid pointerExample_1( void ) {int myInt0 = 100;int myInt1 = 200;int* pMyInt0 = &myInt0;int* pMyInt1 = pMyInt0;*pMyInt0 += 20;*pMyInt0 += 20;*pMyInt1 += 40;cout << myInt0 << endl;cout << myInt1 << endl;cout y t e d ;cout << *pMyInt0 << endl;cout << *pMyInt1 << endl << epMyInt1 = &myInt1;*pMyInt0 *= 2;*pMyInt1 = myInt0 + *pMyInt0;cout << myInt0 << endl;cout << myInt1 << endl;cout << myInt1 << endl;cout << *pMyInt0 << endl;cout << *pMyInt1 << endl << e}: print to the console?160200160160160320ndl;640320640ndl;8Pointers – Example 2● What will the following functionvoid pointerExample_2( void ) {const int arraySize = 5;int myIntArray[ arraySize ] =yy[y]myIntArray[ 1 ] = myIntArray[int* myPtr = myIntArray;++( *myPtr );++myPtr;***( 1)*myPtr *= *( myPtr + 1 );myPtr += 2;myPtr[ 1 ] *= myPtr[ 0 ];*myPtr -= 1;// displayfor ( unsigned inti = 0; i <cout << myIntArray[ i ] }}}: print to the console?= { 2, 0, 6, 8, 10 };324{,,,, }; 0 ] * 2;6780 arraySize; ++i ) {<< endl;9Pointers – Array dereferenci● Array subscript operator ( [dereferencing.● C++ pointers are strictly typointer point to a char poinint myInt = 42;int* pMyInt = &myInt;char c = 'c'char* pMyChar = &c;pMyInt = pMyChar;:ing, type independence[] ) is a form of indexed yped; can't have an intnter, for example.10Pointers – Typecasting● We can, however, cast oneint myInt = 42;int* pMyInt = &myInt;char c = 'c'char* pMyChar = &c;py ;// pMyInt = pMyChar;pMyInt = reinterpret_cast< Th l t t●There are several typecastsreinterpret_cast could be indangerous. static cast is sg_in which it won't do.● Old C-style casts like pMyInypywork, though. These are li: data type to another:int* >( pMyChar ); d s you can do. nterpreted as the most safer, but there are situations ,nt = (int*)pMyChar will still ()pyke reinterpret_cast<>.11Pointers and new● Of course, pointers start tonew, when we allocate memint* pMyIntAry = new int[ 1pMyIntAry[ 25 ] = 1001;// ... do some interesting delete[] pMyIntAry;: become really useful with mory on the heap:1000 ];stuff here12Pointers to pointers (to poin● We can also have pointers int myInt = 42;int* pMyInt = &myInt;int** ppMyInt = &pMyInt;// int*** pppMyInt = &ppMyI● Question: when might this ● Answer: implementing dynamQuestion: could we do somimplementing pass-by-referenstrings, command-line options●Question: could we do som&&myInt ? What about intAnswer: &&myInt will generat●Answer: &&myInt will generattake the address of an addresthough. It evaluates to myIn:nters...)to pointers:Int; be useful?mic multi-dimensional arrays, mething like int** ppMyInt nce for pointers, arrays of s e.g. main (int argc, char **argv)mething like int** ppMyInt = t myInt2 = **ppMyInt ?te an error because you can't te an error, because you can t ss. **ppMyInt will work, t. (What about *ppMyInt ?)13The Ternary Operator (cond● The ternary operator (condz. Can loosely translate terif-then statements of the foif ( x ) {“return”y;} else {“return” z;}● Not really doing a return; thinking about it is “evaluatthinking about it is evaluat“evaluate this expression a:ditionals)ditional) is of the form x ? y : rnary-form statements into orm:perhaps a better way of te this expression as y” and te this expression as y and s z.”14The Ternary Operator – a simvoid ternaryOperatorTest_0( unsigncout << "I am taking "<< classCount<<"l "<< " class"<< ( ( 1 == classCount<< " this semester."<}void main( void ) {ternaryOperatorTest_0( 0 );ternaryOperatorTest_0( 1 );ternaryOperatorTest_0( 2 );ternaryOperatorTest_0( 3 );ternaryOperatorTest_0( 4 );}Ot tOutput:I am taking 0 classes this semesteI am taking 1 class this semester.I am taking 2 classes this semesteI am taking 3 classes this semesteI am taking 4 classes this semeste:mple examplened int classCount ) {t ) ? "" : "es" )<< endl;er..er.er.er.15File I/O: writing to files with ● Writing text data to files is very euse the same familiar syntax as void fileOutputExample_0( void ) {ofstream myOutputFile;myOutputFile.open( "my_file.txt"if ( false == myOutputFile.is_opcerr << "ERROR: Unable to return;}}myOutputFile <<
View Full Document