06-02-2008, 10:34 AM
There is one significant difference between a pointer and an index: you can only compare pointers within the same aggregate object (or with NULL). So you can check if an integer index is within the range of an array, but given a pointer you can't check that the pointer is in a valid range (unless you invoke the undefined behaviour in ANSI C90 6.3.8).
For example, given:
you can do:
but you can't (portably) do:
Is this important? I suspect not very much in practice.
stephen
For example, given:
Code:
int32 a[N];
you can do:
Code:
int32 lookup(int32 v)
{
int32 r;
if ((v < 0) || (v >= N))
{
/* handle error. */
r = 0;
}
else
{
r = a[v];
}
return r;
}
but you can't (portably) do:
Code:
int32 lookup(int32 const *vp)
{
int32 r;
if ((vp < a) || (vp >= (a + N))
{
/* handle error. */
r = 0;
}
else
{
r = *vp;
}
return r;
}
Is this important? I suspect not very much in practice.
stephen