c programming style

M.J.Shannon mjs at eagle.UUCP
Sat Jul 20 22:59:04 AEST 1985


> To my trained and experienced mind, incrementing
> and adding a constant are very different things (and C programmers and
> language developers are not gods :-).
> -- 
>  - joel "vo" plutchak

The theory is this: if you declare a variable to point to a particular type of
object, you *should* be manipulating objects.  The compiler (or, more
precisely, the language specification) states that arithmetic on the pointer is
done in multiples of the object's size.

Since the expression *(E1+E2) is semantically equivalent to E1[E2], it is very
reasonable that the value of either expression is the object E2 ahead of E1, no
matter how big the object is.  Even fortran and pascal define the semantics of
indexing the same way.  Language consistency demands that pointer arithmetic be
defined as it is.

Yes, you can bitch & moan to your heart's content, but I (for one) will refuse
to (linearly) step through a symbol table like this:

	struct symtab {
		char * name;
		long value;
		int flags;
	};
	struct symtab symtab[1024];
	...
	for (sp = &symtab[0]; sp->name != NULL; sp += sizeof (struct symtab))
		...

when I should be able to say:

	...
	for (sp = &symtab[0]; sp->name != NULL; ++sp)
		...

(For nit-pickers, yes, there should be tests for reaching the end of the symbol
table, but the point here is to illustrate pointer manipulation.)
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063



More information about the Comp.lang.c mailing list