Look at this code ptr int x1 y2 z int p p p x x z

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Object Programming *ptr iis the value stored at the address s stored in ptr. Look at this code: ptr int x=1, y=2, z; int* p; p p = &x &x; z = *p *p; p = &y &y; z += *p; cout << z; *ptr iis the value stored at the address s stored in ptr. Look at this code: ptr x 1 y 2 z 1 p iis set to point at y s int x=1, y=2, z; int* p; p p = &x &x; z = *p *p; p = &y &y; z += *p; cout << z; 1 y 2 z 1 iincrement z by the value pointed at by p. ncrement by Object Programming *ptr iis the value stored at the address s stored in ptr. Look at this code: ptr int x=1, y=2, z; int* p; p p = &x &x; z = *p *p; p = &y &y; z += *p; cout << z; x x 1 y 2 z 3 iincrement z by the value pointed at by p. ncrement by Object Programming W hen we use the * to refer to the contents of where a pointer points, it is called dereferencing dereferencing the pointer. NOTE that a 0-valued pointer cannot be valued dereferenced as it doesn't point anywhere. dereferenced Care must be taken to ensure that a pointer has a Care valid value before it is dereferenced. dereferenced Object Programming If we return to our sort routines for a moment, we can now replace our Index array of ints by an Index array int by array of pointers. array So Object Programming int CompareData(int IV1, int IV2) int IV1, int { // compare *Index[IV1] to *Index[IV2] } and void ExchangeData(int IV1, int IV2) void ExchangeData(int IV1, int { // exchanges Index[IV1] and Index[IV2] } DataType X[SIZE]; DataType* Index[SIZE]; Index[SIZE for (i=0;i<n;i++) Index[i] = &X[i]; Of course, our two functions change also. Object Programming Note that we still refer to the pointers by integer Note indices and hence the sort functions are still generic. generic. Object Programming However, it should also be noted that pointers to different types are considered different pointer types. types. What space is used for a pointer? We'll see soon how we can find this out. For the moment let's just say it's about the size of For an int (4 bytes). int An advantage of the pointer approach to sorting is that the data need not be in an array. is double SalesSpr, SalesSum, double SalesSpr SalesSum SalesAut, SalesWin; SalesWin double* SalesIndex[4] = {&SalesSpr, &SalesSum,&SalesAut,&SalesWin}; allows us to sort the four values where they are – allows we are still not moving them of course. Object Programming Object Programming You will find very quickly that these * floating around get very confusing. around It's better to create a pointer type alias. It's pointer This is useful for passing pointers to functions, especially when passing by reference. especially For example, Hey, isn't passing by reference just the placing of Hey, the address of the argument on the stack? the typedef DataType* DataPtr; DataPtr and then use the type to create pointer variables That is, the formal argument of a function is an That alias to the actual argument. alias C doesn't have that construct. DataPtr Index[10]; Object Programming Consider the swap function for exchanging two integer values. In C++ this is: integer void Swap(int& X1, int& X2) void Swap(int X1, int { int temp; The addresses of the actual The arguments A and B...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online