A nice macro

Norman Diamond diamond at diamond.csl.sony.junet
Fri Jun 23 13:00:13 AEST 1989


In article <2784 at solo8.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>>C arrays always begin with subscript 0. ... the solution below seems so
>>straightforward:
>>	bar	_foo[HIGH - LOW + 1];
>>	#define		foo		(_foo - LOW)

In article <13788 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:

>If LOW <= 0 <= HIGH, no problem.

Well, as long as LOW <= 0 and LOW <= HIGH.  The latter is trivially a
requirement of all languages' array support.  This solution also works
for LOW == -5 and HIGH == -3.

>But this is not portable if you're trying to emulate, say, origin-1
>arrays (LOW==1), since the expression (_foo-1) could be >outside your
>address space.

True indeed.  The entire expression would yield a valid location
(except when the program really does have a subscript error),
e.g. foo[i] == *((_foo-LOW)+i) would be legal if the compiler were
permitted to rewrite the expression as *(_foo+(i-LOW)).

Looks like ANSI has standardized existing practice once again, by
forbidding compilers from making such a rearrangement.  If permitted,
maybe this would have become a quality-of-implementation issue.

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp at relay.cs.net)
 The above opinions are claimed by your machine's init process (pid 1), after
 being disowned and orphaned.  However, if you see this at Waterloo, Stanford,
 or Anterior, then their administrators must have approved of these opinions.



More information about the Comp.lang.c mailing list