Sunteți pe pagina 1din 16

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; // integer x contains value '7'
int *xp; // 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; // declares xp as a pointer to an integer
xp = &x; // xp receives the address of 'x'
cout < xp; // 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; // xp receives the address of 'x'
cout < *xp; // outputs value contained by 'x'
*xp = 9; // 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; // equivalent to long *lp = &x[0];
*lp = 100; // x[0] now contains 100
lp++; // lp now points to x[1]
*lp = 200; // 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 <alloc.h>

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 handsonc@ePublicist.ca with
"C Pointers Demo" in the subject line

ePublicist.ca © 2007, 2008, 2009

S-ar putea să vă placă și