Pointers in C
Pointers in C
Posted Nov 4, 2007 19:07 UTC (Sun) by tialaramex (subscriber, #21167)In reply to: Many good points by pynm0001
Parent article: Daniel Bernstein: ten years of qmail security
You /can/ point anywhere but that isn't defined in the language and so your compiler might not
do what you expected. It so happens that the pointers are typically just hardware memory
addresses (virtual addresses on modern hardware) but they could be anything, and any false
assumptions you make in portable software could be expensive mistakes.
K&R says that pointers are only defined when they point /to/ something like an array element
or a variable. ANSI C improved on this by asserting that there is also a pointer value beyond
the end of an array which is larger than the pointer values for the elements of the array,
this means that...
while (pointer <= last_element)) {
/* do something */
pointer++;
}
is well defined in ANSI C and does what you expect whereas it would have been legitimate for a
K&R C compiler to do something most unexpected, like set the pointer variable to zero once you
get beyond the end of the array.
