Sie sind auf Seite 1von 11

Department of Computer and Information Science,

School of Science, IUPUI

CSCI 230

Pointers
Pointer Arithmetic
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: droberts@cs.iupui.edu

Dale Roberts

Pointer Expressions and Pointer Arithmetic


pv+n
pv + n*sizeof(variable type that pointer point to)
Arithmetic operations can be performed on pointers
Increment/decrement pointer (++ or --)
Example:

++vPtr, vPtr++,
--vPtr, vPtr--

Add an integer to a pointer( + or += , - or -=)


Pointers may be subtracted from each other
Operations meaningless unless performed on an array

Dale Roberts

Example:
Five element int array on machine with 4 byte
ints
vPtr points to first element v[ 0 ]whose address
location is 3000 (vPtr = 3000)
vPtr += 2;

// sets vPtr to 3008


vPtr points to v[ 2 ] (incremented by 2), but
the machine has 4 byte integers, so it points to
location
address 3008
3000 3004 3008 3012 3016
pointer variable
vPtr

v[0] v[1]

Dale Roberts

v[2]

v[3]

v[4]

Pointer Expressions and Pointer Arithmetic


Example: (double pointer)
Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes.
long a[10]={5, 10, 15, };
long *pa, **ppa;
int i=5;
pa = &a;
a
ppa = &pa;
640 5
800
700

ppa

644 10
648 15
652
656
660

700
640

pa

Variable

Address

Value

640

644

10

648

15

pa

700

640

ppa

800

700

Questions:

Expression

Value

Note

pa+1

644

640+1*4

pa+3

652

640+3*4

pa+i

660

640+i*4

ppa+1

702

700+1*2

ppa+i

710

700+i*2

*pa+1

5+1

*(pa+1)

10

a[1]=pa[1]=*(a+1)

pa[2]

15

648

*ppa

640

value of pa

*ppa+1

644

pa+1

*(ppa+1)

invalid

*(702)

**ppa+1

a[0]+1 = 5+1

*(*ppa+1)
Dale Roberts

10

*(pa+1)=*(640+1*4)

Pointer Expressions and Pointer Arithmetic


Subtracting pointers
Returns number of elements from one to the other. If
vPtr2 is a pointer pointing to v[ 2 ];
vPtr is a pointer pointing to v[ 0 ];

vPtr2 - vPtr would produce 2

Pointer comparison ( <, == , > )


See which pointer points to the higher numbered array element
Also, see if a pointer points to 0

Pointers of the same type can be assigned to each other


If not the same type, a cast operator must be used
Exception: pointer to void (type void *)
Generic pointer, represents any type
No casting needed to convert a pointer to void pointer
void pointers cannot be dereferenced

Dale Roberts

The Relationship Between Pointers and Arrays


Arrays and pointers are closely related
Array name like a constant pointer
Pointers can do array subscripting operations
Example: Declare an array b[ 5 ] and a pointer bPtr

bPtr = b;
//
//

To set them equal to one another


The array name (b) is actually the address of first element of the array

bPtr = &b[ 0 ];
// Explicitly assigns bPtr to address of first element of b

To access element b[ 3 ]:

x=*( bPtr + 3 ) //
x=bptr[ 3 ]
//

Where n is the offset. Called pointer/offset notation

Called pointer/subscript notation


// bPtr[ 3 ] same as b[ 3 ]

x=*( b + 3 )

// Performing pointer arithmetic on the array itself

Dale Roberts

Pointers and Arrays


Strong relation between pointers and arrays
Pointers and arrays can be used interchangeably.
The array name is equivalent to the address of the first
element in the array
Example:
int a[10];
int *pa;
pa = &a[0]; /* is equivalent to pa = a */
So,
a[1]
*(pa+1)
pa[1] *(a+1)
&a[1]
pa+1
a+1
a[i]
*(pa+i)
pa[i] *(a+i)
&a[i]
pa+i
a+i
a[i]+=5 *(pa+i)+=5
pa[i]+=5
Example:
f(int *s)
f(int s[])
{
{
}
}

Dale Roberts

Arrays of Pointers
Arrays can contain pointers
For example: an array of strings
char *suit[4] = {"Hearts", "Diamonds",
"Clubs", "Spades"};
Strings are pointers to the first character
char * each element of suit is a pointer to a char
The strings are not actually stored in the array suit, only pointers to
the strings are stored
suit[0]

\0

suit[1]

suit[2]

\0

suit[3]

\0

\0

suit array has a fixed size, but strings can be of any size
Dale Roberts

Pointers to Functions
Pointer to function
Contains address of function
Similar to how array name is address of first
element
Function name is starting address of code
that defines function

Function pointers can be


Passed to functions
Stored in arrays
Assigned to other function pointers
Dale Roberts

Example: Bubble Sort


#define SIZE 10
void bubble(int [],const int,
int (*)(int,int));
int ascending( int, int );
int descending( int, int );
int main() {
int a[SIZE] = { 2, 6, 4, 8, 10,
12, 89, 68, 45, 37 };
bubble( a, SIZE, ascending );
bubble( a, SIZE, descending );
}

void swap( int *element1Ptr,


int *element2Ptr ) {
int temp;
temp = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = temp;
}
int ascending( int a, int b ) {
return b < a;
/*swap if b is less than a*/
}
int descending( int a, int b ) {
return b > a;
/*swap if b is greater than a*/
}

void bubble( int work[], const int size,


int (*compare)(int,int)) {
int pass, count;
void swap( int *, int * );
for (pass=1; pass<size; pass++)
for (count=0; count<size-1; count++)
if ((*compare)(work[count],work[count+1]))
swap(&work[count],&work[count + 1]);
}

Dale Roberts

Function bubble takes a function pointer


bubble calls this helper function
this determines ascending or descending sorting

The argument in bubblesort for the function


pointer:
bool ( *compare )( int, int )
tells bubblesort to expect a pointer to a function that
takes two ints and returns a bool

If the parentheses were left out:


bool *compare( int, int )
Declares a function that receives two integers and
returns a pointer to a bool
Dale Roberts

Das könnte Ihnen auch gefallen