Pointers in 'C'
ePublicist.ca © 2007, 2008, 2009
Introduction
Pointers are a special kind of variable To use these new operators are needed Pointers will permit our programs to look 'around corners' Most importantly pointers will permit us to manage memory allocated on the 'heap'
ePublicist.ca © 2007, 2008, 2009
What is a Pointer?
Regular variables contain values Pointers are variables that contain the address of another variable As a result we say that a pointer "points to" or "references" another variable
ePublicist.ca © 2007, 2008, 2009
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;
// integer x contains value '7' // declares xp as a pointer to // an integer ePublicist.ca © 2007, 2008, 2009
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; xp = &x; cout < xp;
// declares xp as a pointer to an integer // xp receives the address of 'x' // outputs address of 'x's location
ePublicist.ca © 2007, 2008, 2009
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; cout < *xp; *xp = 9;
// xp receives the address of 'x' // outputs value contained by 'x' // changes value contained by 'x' to 9
ePublicist.ca © 2007, 2008, 2009
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 // to to var xp points to *yp = temp; // assign saved value to var yp } // points to ePublicist.ca © 2007, 2008, 2009
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 cout < iarray[0]; // outputs 5 cout < *iarray; // outputs 5 ePublicist.ca © 2007, 2008, 2009
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; *lp = 100; lp++; *lp = 200;
// equivalent to long *lp = &x[0]; // x[0] now contains 100 // lp now points to x[1] // x[1] now contains 100 ePublicist.ca © 2007, 2008, 2009
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:
ePublicist.ca © 2007, 2008, 2009
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' } ePublicist.ca © 2007, 2008, 2009
Dynamic Memory Allocation
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); ePublicist.ca © 2007, 2008, 2009
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
ePublicist.ca © 2007, 2008, 2009
Malloc Function Demo Code Example: #include <stdio.h> #include
void main() { int toal, *ap; int total = 50; ap = (int *)malloc( total * sizeof(int)); if(ap ==NULL) { printf("Failed to allocate memory...\n"); return; } } ePublicist.ca © 2007, 2008, 2009
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; ePublicist.ca © 2007, 2008, 2009
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 For a copy of all demo files included in this presentation write [email protected] with "C Pointers Demo" in the subject line ePublicist.ca © 2007, 2008, 2009