Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!umd5!uflorida!novavax!proxftl!bill From: bill@proxftl.UUCP (T. William Wells) Newsgroups: comp.misc Subject: Re: Programming style with arrays, summary Summary: go ahead, USE those templates ... here's how. Keywords: programming style, arrays Message-ID: <337@proxftl.UUCP> Date: 18 Jun 88 22:01:15 GMT References: <778@dick.cs.vu.nl> Organization: Proximity Technology, Ft. Lauderdale Lines: 26 In article <778@dick.cs.vu.nl>, dick@cs.vu.nl (Dick Grune) writes: ) Two people argue convincingly that using the number of elements and ) using the address of the last+1 item is actually the same thing. They advocate ) tests like: ) ) if (p >= &array[N]) ) error(...); ) ) and loops like: ) ) for (p = &array[0]; p < &array[N]; p++) ) process(*p); ) ) both of which look sane and understandable. I seriously thought about adopting ) these "templates" systematically in my coding, when I realized that for it to ) work, you would have to declare N as a long. Otherwise you lose on machines ) with 2-byte ints and 4-byte pointers. The new C standard has a fix for this. One can even use this with current code. Declare any variable which contains the size of an object as of type size_t. If you are using Standard C, include ; if not, add to your program an appropriate typedef for it. K&R compatible compilers would use typedef unsigned size_t; compilers with longer addresses would use typedef unsigned long size_t. (There is an equivalent type for the difference between two pointers, ptrdiff_t; I suggest using this as well.