Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!rochester!uhura.cc.rochester.edu!ur-valhalla!badri From: badri@valhalla.ee.rochester.edu (Badri Lokanathan) Newsgroups: comp.lang.c Subject: Re: Contiguous Arrays Summary: Consistency with K&R1. Message-ID: <1831@valhalla.ee.rochester.edu> Date: 22 Feb 89 21:22:33 GMT References: <2508@ssc-vax.UUCP> <8943@alice.UUCP> <1828@valhalla.ee.rochester.edu> <1989Feb22.171441.7957@utzoo.uucp> Organization: UR Dept. of Electrical Engg, Rochester NY 14627 Lines: 46 > In article <1828@valhalla.ee.rochester.edu>, I said > >For instance, if the array was to be x[101] to x[110] instead of > >x[0] to x[9], what is the easiest way to do it? > >For automatic arrays one could do > >int space[10], *x; > >x = &space[0] - 101; In article <1989Feb22.171441.7957@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes: > No one couldn't, not if one wants to be portable. There is absolutely no > guarantee that x[101] will be the same as space[0], and in fact there is > no guarantee that the computation of x won't trap and give you a core dump. > On a sufficiently odd segmented machine it might. Even K&R1 warns you that > pointer arithmetic won't necessarily do what you think unless you keep it > within an array. First, a disclaimer. My suggestion was just a possibility: personally I would never use it: if at all, I would perform preprocessor abuse. Now to Henry's reply: Henry is probably referring to the comments on pages 98 and 189 of K&R1. My argument is, since x is an independent variable (a pointer, but still a variable,) there is no way of checking any array bounds. Thus in order for consistency with the definition of pointer arithmetic, x -= i; x += i; should result in the original value of x (provided the resulting intermediate values are within pointer ranges.) Since x can be initialized to any value, this should always be possible, the result after each operation being an address appropriately offset from the original object. Yes, accessing the contents of an illegal location will almost certainly screw things up, but there is no access happening. Further, the + and - operations group left to right and are associative. Note that in C, a[i] is completely equivalent to *(a+i) if a is a pointer (page 94.) But Henry's statement implies that on some architectures (x + i) - i != x + (i - i) Sigh. Are we biblical scholars or what? -- "Don't blame me for wanting more {) badri@ee.rochester.edu The facts are too hard to ignore //\\ {ames,cmcl2,columbia,cornell, I'm scared to death of poverty ///\\\ garp,harvard,ll-xn,rutgers}! I only want what's best for me."-UB40 /\ rochester!ur-valhalla!badri