PART I
C h a p t e r 2 0 :
U n s a f e C o d e , P o i n t e r s , N u l l a b l e T y p e s , a n d M i s c e l l a n e o u s T o p i c s
593
PART IPART I
p[4]: 4
p[5]: 5
p[6]: 6
p[7]: 7
p[8]: 8
p[9]: 9
Use pointer arithmetic.
*(p+0): 0
*(p+1): 1
*(p+2): 2
*(p+3): 3
*(p+4): 4
*(p+5): 5
*(p+6): 6
*(p+7): 7
*(p+8): 8
*(p+9): 9
As the program illustrates, a pointer expression with this general form
*(ptr + i)
can be rewritten using array-indexing syntax like this:
ptr[i]
There are two important things to understand about indexing a pointer: First, no
boundary checking is applied. Thus, it is possible to access an element beyond the end
of the array to which the pointer refers. Second, a pointer does not have a
Length
property.
So, using the pointer, there is no way of knowing how long the array is.
Do'stlaringiz bilan baham: |