referencing addresses, pointers

Click here to load reader

Upload: nusa

Post on 22-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Referencing addresses, Pointers. Pointer definitions, usage, operations, arrays of pointers, function pointers. The Pointer Concept and Usage in C. Concept of Pointer as memory address Pointer definitions Usage of pointers Operations on pointers Function arguments: Call by Value/Reference - PowerPoint PPT Presentation

TRANSCRIPT

Array Data Structures & Algorithms

Pointer definitions, usage, operations, arrays of pointers, function pointersReferencing addresses, Pointers1The Pointer Concept and Usage in CConcept of Pointer as memory addressPointer definitionsUsage of pointersOperations on pointersFunction arguments: Call by Value/ReferenceArrays of pointers and Pointer types of FunctionsThe const qualifierPointers to Functions2Concept of Pointer as memory addressAll programs exist in RAM when executingData and CodeVariables and data structures (eg. arrays) are allocated memory locations (addresses)In static blocks, in stack frames, on the heapIt is often useful to exploit knowledge of data structures along with location, or address, dataIt is the ability to define a variable that stores address values, coupled with the ability to indirectly access meaningful data values, that makes pointers useful.3Concept of Pointer as memory addressFor program codes produced by a compiler, RAM is partitioned into blocks of memory according to what kind of data is placed in the blocksExecutable instructions (code)DataStacksHeapsBuffersFor programmer defined variables, names are used int intVarname = 64 ; float fltVarName = 3.14159 ;Although we usually think of the value stored in a variable, the variable name actually refers to a RAM address (at which the value is stored)However, the names of scalar variables do not correspond directly to their address location valuesintVarname does not have a value !CODEDATABUFFERSSTACKSHEAPCode Block AddressData Block AddressHeap Block AddressStack Block AddressBuffer Block AddressintVarnamefltVarname643.14159Pointer definitionsC defines two pointer related operators * - dereferencing (asterisk) & - address_of (ampersand)

Examples: // Variable name for an integer storage with value 5 int Varname = 5 ;

/* Variable name for a pointer-to-integer storage with value set to the RAM address of Varname */ int * ptrVarname = &Varname ;The strongly typed nature of the C language dictates that all pointers must be type-specific.

Each pointer variable is said to point to a RAM address where it is expected that data of a compatible type is stored.

Any type incompatibility will be reported as a compiler error.5Usage of pointersDeclarations int N, *ptrN ; // N is an int variable, ptrN is pointer double * X, * Y ; // Must always use * for each pointer

Assigning values ptrN = &N ; // Use the address-of operator to get the RAM address X = Y ; // Can assign type compatible pointers *X = 24.53 ; /* Use dereferencing to assign compatible values that will be stored at the RAM location X is pointing to */ *Y = *X ; /* Copy the value stored where X is pointing to where Y is pointing */WARNING!Be careful to separate the notion of data value from memory addressVariables that hold meaningful data provide direct access to RAMPointers that hold address data provide indirect access to RAM when dereferencing them to get at meaningful data6Usage of pointersThere is a special constant that deserves emphasis NULL

If a pointer variable is not assigned a value then it cannot referenced. It is useful to have a value that can be referenced, but which indicates no particular value of the address int * ptr = NULL ;

This provides advantages in programming logic because one can test the value of a pointer if ( ptr != NULL ) { /* perform logic */ }

We will see much more use of NULL pointers in future discussion of dynamic linked lists and other advanced data structures7Operations on pointersThe following operations are meaningful for pointers and define pointer arithmeticAssume the declarations: int aN [100] ; char aC [256] ; double aD [ 1000 ] ; int * pN ; char * pC ; double * pD ;The following are valid statements: pN = aN ; pC = aC ; pD = aD ; // Array names are pointers !! pN = &aN[0] ; pC = &aC[0] ; pD = &aD[0] ; // Equivalent to above

pN = pN + 1 ; // Points to aN[1] pN++ ; ++pN ; // . and then to aN[2], then aN[3] pN -= 3 ; // . and now back to aN[0]

Note above that we have used +, -=, and ++ All arithmetic operations on pointers must involve integer addition or subtraction only !8Operations on pointersPointers are often used with arrays. Assume the following: float A[1000], * ptrA = &A[0], * ptrB = &A[49] ;

Consider the following equivalent statements A[5] = 6.73 ; ptrA[5] = 6.73 ; *(ptrA+5) = 6.73 ;

Now consider a problem. Using pointer arithmetic only, determine the array subscript corresponding to the position of the pointer relative to the beginning of the array:Assume: int Subscript ;Answer: Subscript = ( ptrB ptrA ) / sizeof( float ) ; // the value above is 49Note that pointers can be subtracted from each other, but not added!

If A, B, C and D are compatible pointers, then consider the expressions:

A B OKA + B ERR

A B + COKA + B CERR

A B + C D OKA + (B - C) OK

(A B) + (C D) OK A B D + C ERR9Arrays of pointersProblem:Sort a 2D array in ascending order on first column: double X[1000][50000] ;Note that the size of the rows is immense so exchanging rows will take a lot of processing timeAnswer:Use an array of pointers: double * pX[1000], * tptr ;Initialize each element to point to the beginning of each row for( k=0; k