Fesq, 3/14/01 8 16.070
Pointer Arithmetic - Addition
• Pointers can be operated on with integer arithmetic
! For pointer addition, result is a new value of the pointer, which points to a
new address
ptr = ptr + n;
! Will point to the address of the memory location n after the original
! Physical memory address will depend on the type of variable the pointer is
associated with (C compiler knows this from the declaration)
! C will automatically scale the pointer by: n x (# of bytes per variable)
Address
ptr = 1000
Char
(1 byte)
Integer
(2 bytes)
Float/Long
(4 bytes)
Double
(8 bytes)
ptr 1000 1000 1000 1000
ptr + 1 1001 1002 1004 1008
ptr + 12 100C 1018 1030 105F
! The arithmetic on the pointer does not change the value stored at either the
old or the new location (but does change value of pointer)
!
!Programmer must ensure pointer arithmetic is within a single data type