Path: utzoo!attcan!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Pointer incrementation and assignment Message-ID: <1704@auspex.auspex.com> Date: 26 May 89 19:15:17 GMT References: <2015@csuna.csun.edu> Reply-To: guy@auspex.auspex.com (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 44 >But would doing this: > > a += 1; > >increase a by 1 or by 2? It would increment the value of the variable "a" by 1, just as would "a++". That is, it would cause "a" to point to an element one further along in the (putative) array into which it points. In other words, on your example machine, it would increment the address stored in the variable "a" by 2. >Or how about > > a = a + 1; Same thing. "a++", "a += 1", and "a = a + 1" all add 1 to the value of "a", regardless of the type of "a"; it's just that the meaning of "add 1 to the value of 'a'" differs depending on the type of "a". If "a" is of integral type, its value plus 1 is the successor to the integer that is its value (modulo - no pun intended - overflow or modular-arithmetic wraparound for unsigned types). If "a" is of floating-point type, its value plus 1 is the sum of its current value and 1.0, given the implementation of floating-point arithmetic on your machine (taking into account overflow, loss of significance of the "1.0" - i.e., 1.0e36 + 1.0 probably isn't 1.000......01e36 unless you have lots of bits of precision). If "a" is of pointer type, its value plus 1 is a pointer to the element following the one pointed to by "a" in the (putative) array to which the element pointed to by "a" belongs. (If the value of "a" is represented by the number N - i.e., "a" points to something starting at the Nth byte, 0-origin of course, in your address space - the value of "a+1" is represented by the number N+sizeof (*a). Be careful *not* to just think of this as "adding 'sizeof(*a)' to 'a'.") Think of it in those terms, rather than, say, thinking of "a++" as shorthand for "part of how you get at your machine's autoincrement address mode", and it becomes clearer (*and* more machine-independent - not all machines *have* an autoincrement addressing mode, for instance).