DOC PREVIEW
LETU COSC 2103 - Pointers and Strings

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Pointers and StringsWhat You Will Learn . . .IntroductionPointer Variable Declarations and InitializationsPointer InitializationPointer OperatorsSlide 7Slide 8Slide 9Calling Functions by ReferenceSlide 11Slide 12Using the const Qualifier with Pointersconst Used on ParametersSlide 15Bubble Sort Using Call-by-referenceBubble Sort Using Call-by-reference -- Note ...Pointer Expressions & Pointer ArithmeticSlide 19Slide 20Pointer Expressions & Pointer Arithmetic -- warnings !!Pointers to void ( void * )Relationship Between Pointers and ArraysSlide 24Slide 25Arrays of PointersSlide 27Function PointersSlide 29Slide 30Slide 31Characters and StringsStringsSlide 34String Manipulation Functions1Pointers and StringsChapter 52What You Will Learn . . . How to use pointersPassing arguments to functions with pointersSee relationship of pointers to strings and arraysPointers can point to functionsDeclaring & using arrays of strings3IntroductionPointer variables contain memory addresses as their valuesContrast normal variables which contain a specific valuex holds a valuexptr holds memory address of some int locationint *xptr, x;x 5xptr4Pointer Variable Declarations and InitializationsMust declare the type of variable to which pointer variable will pointMust use the leading * to specify this is a pointer variableGood idea to include ptr in the name of the pointer variablefloat amt, *fptr;5Pointer InitializationWhen declared, variables should NOT be assumed to have any certain valueAs with all variables, pointer variables should be explicitly initialized–Initialized with address of a specific variable–Initialized to 0 or NULL (points to nothing)6Pointer OperatorsAddress operator &–unary operator–returns address of operandint *xptr, x;x = 5;xptr = &x;x 5xptr7Pointer OperatorsAddress operator &–unary operator–returns address of operandint *xptr, x;x = 5;xptr = &x;x 5xptr 123412346500AddressValues actually stored8Pointer OperatorsThe indirection or dereferencing operator–Use the * symbol *xptr will yield the value stored at the location to which xptr pointsint x, *xptr;x = 50;xptr = &x;cout << *xptr;// what gets printed?509Pointer OperatorsThe precedence of –& the address operator–* the dereferencing operatorHigher precedence than multiplication and divisionLower precedence than parentheses10Calling Functions by ReferenceThree ways to pass arguments to a function–call by value–call by reference with reference parameters–call by reference with pointer parametersvoid doit (int amt);void whatever ( float &value);void fribble (char *ch);11Calling Functions by ReferenceCall by value–value gets passed one way (to the function) only–can use constant, expression, or variable in callReference parameter–value passed both ways–must use variable in call–parameter automatically dereferenced locally12Calling Functions by ReferenceMust use address in call –use address operatorfribble (&c1);Must explicitly dereference locally within function–use * operatorvoid fribble (char *ch){ *ch = …. ; }13Using the const Qualifier with PointersRecal that const informs the compiler that the value of a "variable" should not be modifiedThis can also be used on function parameters so that actual parameters are NOT affected by changes in formal parametersvoid try_a_change (const char *s_ptr)14const Used on ParametersWhen used, do not try to alter contents of where pointer points tovoid double_int (const int *num_ptr){ *num_ptr = 2 * *num_ptr;}compiler error 15const Used on ParametersConsider the advantage of passing large data objects (structures, arrays) using points to constant data–saves memory•function does not need to create duplicate data object–saves time•program need not copy large number of bytes to the local object16Bubble Sort Using Call-by-referenceRefer to “Cyber Classroom” CD for author’s description of the programRun the program17Bubble Sort Using Call-by-reference -- Note ...array declared as int *arraynot int array [ ]Parameter size declared as const to enforce sorting function not altering size–when passing an array to function, send size also -- don’t have it built inPrototype for swap included inside bubbleSort -- it is the only function that calls swap18Pointer Expressions & Pointer ArithmeticPointer values are valid operands in expressions–arithmetic–assignment–comparisonNot all such operators are valid with pointer variables19Pointer Expressions & Pointer ArithmeticValid operations on pointers++ - - + += - - = int v[5], *vPtr;vPtr = v; // same as vPtr = &v[0];20Pointer Expressions & Pointer ArithmeticConsider:vPtr += 2; // same as vPtr = vPtr + 2;/* But, beware of vPtr -= 4; Why?21Pointer Expressions & Pointer Arithmetic -- warnings !!Beware use of pointer arithmetic on pointers which do not reference an arrayConsider results of subtracting or comparing two pointers which do not reference the same arrayC++ has no range checking -- if you run off the end of an array, you can be in troublePointers can be assigned to other pointers only if they are pointers to the same type–although typecasting can be used (carefully)22Pointers to void ( void * )This is a generic pointer–can represent any pointer typeAll pointer types can be assigned a pointer to void without castingA pointer to void cannot be be assigned a pointer of another type (without casting)Void pointer cannot be dereferenced(why not??)23Relationship Between Pointers and Arrays// Givenint list [5], *intPtr;intPtr = list; // name of array is a pointer constant, an addressThen we can use eitherlist[2] or *(intPtr + 2)to reference the second element of the arrayThe former is clearer but takes longer to compile24Relationship Between Pointers and ArraysNote how pointer is used to traverse array:int b[] = { 10, 20, 30, 40 };int *bPtr = b; // set bPtr to point to array bfor ( offset = 0; offset < 4; offset++ ) cout << "*(bPtr + " << offset << ") = " << *( bPtr + offset ) << '\n';for (bPtr = b; bPtr < b + 4; bPtr++) cout << *bPtr << '\n';How are these two different?How are these two different?25Relationship Between Pointers and ArraysWhat is wrong with this use of the name of the array? for ( bPtr = b ; b < bPtr + 4; b++ )


View Full Document

LETU COSC 2103 - Pointers and Strings

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Pointers and Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointers and Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointers and Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?