[comp.lang.c] Pointer incrementation and assignme

root@mirror.TMC.COM (05/26/89)

The compiler knows the size of the data that you are working with.
a+=1 and a = a+1 will still increment the pointer by 2.

The same way the compiler knows how to increment ints, floats, longs,
and doubles, it knows how to correctly increment any pointer, no matter
what the syntax that you use.  

In reality
the command a++ is just shorthand for a+=1 and a = a+1.  It was origionally
made for lines like:

	whatchamacallit = whatchamacallit + 1;

Carnigan and Ritchie were programmers who didn't like to type.

jkl@csli.Stanford.EDU (John Kallen) (05/27/89)

In article <236100012@mirror> root@mirror.TMC.COM writes:
>In reality
>the command a++ is just shorthand for a+=1 and a = a+1.  It was origionally
>made for lines like:
>
>	whatchamacallit = whatchamacallit + 1;
>
>Carnigan and Ritchie were programmers who didn't like to type.

Kernighan and Ritchie might be lazy typists, but there's another for
having += and ++ operators: I don't have it in front of me, but in the
Bible there was an example of a hairy expression like:

	yylval[yylval1[i]][yylval2[i]] = yylval[yylval1[i]][yylval2[i]] + 1;

which might very easily get corrupted inadvertently. Now,

	yylval[yyval1[i]][yylval2[i]] ++;

is not only shorter code, but *safer*, and above all MUCH clearer.
Useful little beasties, the assignment and the increment operators.

John.
_______________________________________________________________________________
 | |   |   |    |\ | |   /|\ | John Kallen           
 | |\ \|/ \|  * |/ | |/|  |  | PoBox 11215           Support gun control - 
 | |\ /|\  |\ * |\ |   |  |  | Stanford CA 94309     Kill an NRA member!
_|_|___|___|____|_\|___|__|__|_jkl@csli.stanford.edu___________________________

jkl@csli.Stanford.EDU (John Kallen) (05/27/89)

In article <9135@csli.Stanford.EDU> jkl@csli.stanford.edu (John Kallen) writes:
>	yylval[yylval1[i]][yylval2[i]] = yylval[yylval1[i]][yylval2[i]] + 1;
>
>which might very easily get corrupted inadvertently. Now,
>
>	yylval[yyval1[i]][yylval2[i]] ++;
		 ^ insert an 'l' here....

Looks like my example got corrupted inadvertently :-)	
_______________________________________________________________________________
 | |   |   |    |\ | |   /|\ | John Kallen            
 | |\ \|/ \|  * |/ | |/|  |  | PoBox 11215             "Life. Don't talk to me 
 | |\ /|\  |\ * |\ |   |  |  | Stanford CA 94309        about life."         
_|_|___|___|____|_\|___|__|__|_jkl@csli.stanford.edu___________________________

randolph@ektools.UUCP (Gary L. Randolph) (06/02/89)

In article <236100012@mirror> root@mirror.TMC.COM writes:
-The compiler knows the size of the data that you are working with.
-a+=1 and a = a+1 will still increment the pointer by 2.
-The same way the compiler knows how to increment ints, floats, longs,
-and doubles, it knows how to correctly increment any pointer, no matter
-what the syntax that you use.  
-In reality
What is 'reality'
-the command a++ is just shorthand for a+=1 and a = a+1.  It was origionally
Things are never this simple at deeper levels.
-made for lines like:
-
-	whatchamacallit = whatchamacallit + 1;
-
-Carnigan and Ritchie were programmers who didn't like to type.
 ^^^^^^^^?

I SERIOUSLY doubt that two brilliant minds (*Kerninghan* and Ritchie)
included such a feature because they don't like to TYPE.  Perhaps you
are confusing this with the fact that 'type' was something the original
C tended to ignore.  There have been many pleas lately to keep the S/N
ratio down.  Let's not waste $$$$. (My past does not make me a saint :->)
I recall, from my PDP 11 assembler days, two addressing modes;
autoincrement and autodecrement.  I believe that something of this nature
had *much* more to do with the post and preincrement/decrement operators
than did K&R's typing skills.

                           Gary

henry@utzoo.uucp (Henry Spencer) (06/04/89)

In article <1929@ektools.UUCP> randolph@ektools.UUCP (Gary L. Randolph) writes:
>I recall, from my PDP 11 assembler days, two addressing modes;
>autoincrement and autodecrement.  I believe that something of this nature
>had *much* more to do with the post and preincrement/decrement operators
>than did K&R's typing skills.

I made the same mistake a while ago, and Dennis corrected me:  ++ and --
date to the pdp7 days and the B language, and have nothing in particular
to do with the pdp11 addressing modes.  The 7 had a vaguely similar feature
in the hardware, although it was more difficult to exploit, and this may
have inspired Ken Thompson, but the code-generation aspects were not
significant, since B was an interpreted language.  The operators definitely
started out as a notational convenience.
-- 
You *can* understand sendmail, |     Henry Spencer at U of Toronto Zoology
but it's not worth it. -Collyer| uunet!attcan!utzoo!henry henry@zoo.toronto.edu