"Eric J. Korpela" wrote:
It is not permitted in standard C.
Thats because array notations are nothing more
that pointer arithmetics. That is
why it is valid. It eventually becomes *(A + 10) which is *VALID* like any other
pointer operations in C. Of course, this may SEGV in certain systems. I remember
that * (A - 1) used to have the size of the array that was allocated! Dont know if
that
is true today....
It all depends upon what your definition of valid is. If you use "will
compile without error" then any of the above are valid. If you use "is
specified in the language standard as conforming code" then it is definitely
not valid.
IMHO, and according to the C standard, *(A+10) or A[10] are only valid if A[10]
points to a member of the same object as A[0]. *(A-1) is only valid if
*(A-1) and *A are contained in the same object. Compilers will allow a
programmer to violate the standards, but such violations are done at the
expense of portability. Addition of pointers beyond the end of an object
isn't guaranteed to work, even if you don't dereference them.
I agree with you but I have seen several DDJ articles and books about *(A - 1) as most of
the older C compilers were built from K&R C. I strongly doubt it would work today.
The
reason
why this was done IIRC was because passing arrays as arguments in a function, there were
no
way to determine the size of the array , so this is how a lot of programmers got
the size in runtime. I personally NEVER did this....
Ram
--
,,,,
/'^'\
( o o )
-oOOO--(_)--OOOo-------------------------------------
| Ram Meenakshisundaram |
| Senior Software Engineer |
| OpenLink Financial Inc |
| .oooO Phone: (516) 227-6600 x267 |
| ( ) Oooo. Email: rmeenaks(a)olf.com |
---\ (----( )--------------------------------------
\_) ) /
(_/