cvs commit: hugs98/src errors.c script.c hugs98/src/winhugs General.c IORemap.c Winhugs.h

Ross Paterson ross at soi.city.ac.uk
Wed Oct 5 07:12:17 EDT 2005


On Wed, Oct 05, 2005 at 11:45:27AM +0100, Neil Mitchell wrote:
> > I believe that in C the two are completely equivalent.  (Is that not so
> > in VC++?) I just have a prejudice that &arr[n] should be used when one
> > means a pointer to a single element, and arr+n when one wants a pointer
> > to the array from that position (even though in C they're the same thing).
> 
> As far as I was aware, if they operate on char*, they are equivalent,
> since sizeof(char) = 1. However, if the code is ever switched to using
> unicode characters, which makes it a short*, then b = &b[5] is
> equivalent to b += 5 * sizeof(short), which is b += 10. For the moment
> they are the same, but since unicode support is a distinct possibility
> in the future its best to keep it as easy as we can.

In C, p[n] is defined as *(p+n) (see [1] 6.5.2.1), with addition of
pointers and integers defined as advancing the pointer by n array
elements (6.5.6).  So

	(p = &p[5])  ===  (p = &*(p+5))  ===  (p = p+5)  ===  (p += 5)

whatever the value of sizeof(*p).

[1] ISO/IEC 9899:1999 <http://www.nirvani.net/docs/ansi_c.pdf>,
    but this part was there from the beginning.



More information about the Cvs-hugs mailing list