Pointers and Arrays

davidsen at steinmetz.UUCP davidsen at steinmetz.UUCP
Fri Jul 18 05:35:10 AEST 1986


After thinking about the discussions about using the address operator
on an array by name, I come to the reluctant conclusiont that it SHOULD
be allowed in the new ANSI standard. I have read an understood the
arguments against it, I have spent hours teaching the language and
convincing students that they should not do it, but now I have to
reluctantly say that there are some fairly good reasons why it should
be allowed.

Reason 1: "codification of existing practice"
  If three ports of SysV, 2 of 4.2BSD and three of the largest selling
C compiler for PCDOS represent current practice, then it is legal. I
admit that a few compiler BITCHED about it, but they compiled it.

Reason 2: "modularity and information hiding"
  If I am writing a modular program in which I have the typedefs in an
include file used by the programmers writing the modules, there is no
way to allow them to take the address of an item which is defined by
typedef unless they know that the item is (or isn't) an array.

Example:
  typedef int PARTA[10];
  typedef struct { int x,y; float t[40]; } PARTB;

in a module...
  PARTA source, dest, *whead, work[2];
  PARTB *head, workb;

If PARTA is an array, I must say:
  whead = &dest[0];
while if it's not, I say:
  whead = &dest;

This means that the beauty of having the content of types changable at
some time is no longer present, and every programmer who works with
them, even is s/he never uses the contents (passes addresses, etc, like
FILES) must know the type.

Reason 3: "common sense"
  After five years of teaching C, I have to agree with my students that
it makes no sense to forbid this construct. To take the address of
something use the address operator. I have seen this mistake made by
students from major universities, and graduates of courses taught by
high priced consultants, so it's not just my students.

Moreover, there is already a major peculiarity in the was array names
are handled, as compared to the way pointers work. This is in the
operation of the sizeof operator, which gives the size of a pointer or
sizeof an entire array.

Conclusion: I don't find this very desirable, I just think that it
makes more sense to allow it that not allow it. Hopefully the next
language will do away with arrays, and eliminate the whole problem :>

-- 
	-bill davidsen

  ihnp4!seismo!rochester!steinmetz!--\
                                       \
                    unirot ------------->---> crdos1!davidsen
                          chinet ------/
         sixhub ---------------------/        (davidsen at ge-crd.ARPA)

"Stupidity, like virtue, is its own reward"



More information about the Comp.lang.c mailing list