Professional Documents
Culture Documents
5/19/12
POINTERS
Pointers are variables that contain memory addresses as their values.
A pointer indirectly references a value. Referencing a value through a pointer is called indirection.
5/19/12
Memory can be conceptualized as a linear set of data locations. Variables reference the contents of a locations Pointers have a value of the address of a given location
* * * * *
ADDR11
Contents1 1
ADDR16 5/19/12
Contents1 6
Declaration of a Pointer
To declare a variable a pointer we need a new operator to 'modify' the declaration statement Placing a '*' to the left of a variable name declares that variable to be a pointer
Code Example: int x = 7; int *xp;
Pointer Assignment
To assign an address to a pointer we'll need a new operator, the "address of" operator Once assigned, the pointer will contain the "address of" the assigned variable not it's value
Code Example: int *xp; to an integer xp = &x; of 'x' cout < xp;
Dereferencing a Pointer
To work with the variable the pointer 'references' we'll need yet another operator When used in an executable statement the '*' now means "value of" or "contents of"
Code Example: xp = &x; address of 'x' cout < *xp; contained by 'x' *xp = 9;
// xp receives the
5/19/12
Pointer Parameters
Function parameters can also be pointers The use of pointers as parameters 'extends' the functions ability to affect variables outside it's scope
Code Example: void swap( int *xp, int *yp){ int temp = *xp; // save value of var xp points to *xp = *yp; // assign value of var yp points 5/19/12 // to to var xp points to
Arrays as Pointers
Paradoxically, an array variable is essentially a "locked" or const pointer to the first element of an array
Code Example: int iarray[] = { 5,7,9,11,15}; int *iaxp = iarray; // note no need for '&' cout < *iaxp; // outputs 5 5/19/12 cout < iarray[0]; // outputs 5 cout < *iarray; // outputs 5
Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
5/19/12
Pointer Arithmetic
If the simple expression x + 2 results in a value equal to the contents of the var x plus 2 Then xp + 2 results in the address addressed two integer width after the address contained by xp
Code Example: long x[6]; long *lp = x; // equivalent to long *lp = &x[0]; *lp = 100; // x[0] now contains 5/19/12 100
Array as a Parameter
When we pass an array as an argument to a function we are in essence passing the address of the first element of that array, hence the parameter must be a pointer We can also return a pointer as a function's return value See the following demonstration:
5/19/12
Array as a Parameter
// Array as argument example #include <stdio.h> Char *SkipFirst( char string[]){ Return string+1; } Void main() { char word[] = "array"; char str*; str = SkipFirst( word ); Puts( str ); // outputs 'rray' }
5/19/12
To manage large data structures more effectively we need to store them on the 'heap' The memory allocation functions return the starting address of the memory allocated To calculate the amount of memory we need to request we can use a sizeof operator to tell us the size of a variable or data structure in bytes
Code Example: int int_size = sizeof(int); printf("The size of int is %d\n", int_size);
5/19/12
Malloc Function
The maaloc() function expects an unsigned integer ( size_t ) as an argument. This indicates the number of bytes of memory to allocate It returns an address devoid of type information, in other words a pointer to void ( void* ) In order to accept the returned address we'll need to cast it to the type of pointer we'll be using to reference
5/19/12
Pointers to structs
Pointers to structs permit us to point to the component variables that make up a struct
struct Bike{ float speed; unsigned gears; long lisence; }; struct Bike bk; struct Bike *bkptr = &bk; (*bkptr).speed = 10; bkptr->speed = 10;
5/19/12
Summary
Pointers are powerful extensions to variables We need pointers to be able to work with the heap Pointers also simply the way we work with both arrays and structs
5/19/12