Adding two pointers

Gary D Duzan gdtltr at vax1.acs.udel.EDU
Thu May 11 10:06:37 AEST 1989


In article <1321 at infmx.UUCP> kevinf at infmx.UUCP (Kevin Franden) writes:
=>
=>Assuming that there does exist an application that wnats/needs to add
=>pointers, why won't this 'model' work?
=>
=>	Assume an array a to be made up of elements of
=>	arbitrary size.  Assume further that this array has
=>	100 elements arranged (as we know C does) contiguously
=>	in memory.
=>
=>	if p1 is pointing to element 37 and p2 is pointing to
=>	element 4, I'd like p3 to point to a[37+4] or a[41].
=>	
=>	struct{...}foo;
=>
=>	foo p1,p2,p3;
=>
=>	x=37;
=>	y=4;
=>
=>	p1=a[x];
=>	p2=a[y];
=>	p3=(x+y)*sizeof(foo);
=>
=>
=>I realize that I missed part of this discussion, sorry if this has been
=>posted already, but won't p3 be pointing to the 41st element?  if this
=>is true, this is pointer addition, no?
=>
   No. I believe what you want is a[x+y] . p3 will be the number of
bytes taken up by x+y elements of foo. You also might be thinking
a+x+y . This should be legal and do what you want.
   Sorry if I am incorrect here, but I am a bit new to C myself. This
is a bit odd when you consider the fact that I am trying to put together
a minimal K&R C compiler (don't ask for what machine, you'll just laugh).

					Gary Duzan
					Time  Lord
				    Third Regeneration




-- 
+------------------------------------------+ UUCP: !uunet!udel.edu!vax1!gdtltr
| "Two hearts are better than one." -- Yes | Internet: gdtltr at vax1.acs.udel.edu
| "Don't listen to me; I never do." -- Doctor Who | Other: Relay through CUNYVM 
+-------------------------------------------------+        or something.



More information about the Comp.lang.c mailing list