cjoslyn@bingvaxu.cc.binghamton.edu (Cliff Joslyn) (01/18/90)
Under the kind attention of Chris Torek and others, I dutifully understand that arrays are not pointers and pointers are not arrays. I believe that I understand the true relation between array subscripting and pointer arithmetic and arrays passed as arguments converted to pointers, etc. So I turn to Andrew Koenig's great book /C Traps and Pitfalls/ and read on p. 27: "Only two things can be done to an array: determine its size and obtain a pointer to element 0 of the array. *All* other array operations are actually done with pointers, even if they are written with what look like subscripts. That is, every subscript operation is equivalent to a pointer operation, so it is possible to define the behavior of subscripts entirely in terms of the behavior of pointers". I presume this is accurate, and that e.g. passing an array as a parameter or taking its address are considered array operations which are actually done with pointers. In other words, is the first sentence above literally accurate? Thanks. -- O-------------------------------------------------------------------------> | Cliff Joslyn, Cybernetician at Large, cjoslyn@bingvaxu.cc.binghamton.edu | Systems Science, SUNY Binghamton, Binghamton NY 13901, USA V All the world is biscuit shaped. . .
ark@alice.UUCP (Andrew Koenig) (01/18/90)
In article <2804@bingvaxu.cc.binghamton.edu>, cjoslyn@bingvaxu.cc.binghamton.edu (Cliff Joslyn) writes: > "Only two things can be done to an array: determine its size and obtain > a pointer to element 0 of the array. *All* other array operations are > actually done with pointers, even if they are written with what look > like subscripts. That is, every subscript operation is equivalent to a > pointer operation, so it is possible to define the behavior of > subscripts entirely in terms of the behavior of pointers". > I presume this is accurate, and that e.g. passing an array as a > parameter or taking its address are considered array operations which > are actually done with pointers. In other words, is the first sentence > above literally accurate? On reading this sentence again, I still believe that it is literally accurate. For example: void f(int*); int a[10]; main() { f(a); } A casual way to think of this is that you're passing the array `a' to the function `f'. What's actually happening, though, is that the mere mention of the array `a' immediately yields a value that is the address of the initial (0th) element of `a'. Thus the call f(a); is precisely equivalent to the call f(&(a[0])); in which it is perhaps more readily visible that what is being passed is actually the address of an element. If I really wanted to split hairs, I could have added that you can take the address of an array (but only in an ANSI compiler). That would have confused the main point, though. I do explicitly take the address of an array on page 29 and again several times on page 31. I don't think of taking the address of an array as being an array operation, though. There's nothing special about the fact that it's an array. -- --Andrew Koenig ark@europa.att.com
throopw@sheol.UUCP (Wayne Throop) (01/21/90)
>> From: cjoslyn@bingvaxu.cc.binghamton.edu (Cliff Joslyn) >> "Only two things can be done to an array: determine its size and obtain >> a pointer to element 0 of the array. [...]" >> is the [.. above sentence ..] literally accurate? > From: ark@europa.att.com (Andrew Koenig) > If I really wanted to split hairs, I could have added that you can > take the address of an array (but only in an ANSI compiler). [...] > I don't think of taking the address of an array as being an > array operation, though. There's nothing special about the fact > that it's an array. But there IS something special about it, and that is the automatic promotion of array names to rvalue addresses essentially everywhere. Address-of is a reasonable exception, but it IS an exception, and thus if "sizeof" is an array operation, so should address-of be. Or maybe it is better to class it the other way around... In C there is only ONE array operation, that of yeilding the address of the first element of the array. The subscript operation, which in other languages is an array operation, is in C a pointer arithmetic operation coupled with indirection. In this view, neither sizeof or address-of is an array operation, but are operations that occur for any objects. -- Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw@rti.rti.org
woody@rpp386.cactus.org (Woodrow Baker) (01/21/90)
In article <10347@alice.UUCP>, ark@alice.UUCP (Andrew Koenig) writes: > > initial (0th) element of `a'. Thus the call > > f(a); > > is precisely equivalent to the call > > f(&(a[0])); > > in which it is perhaps more readily visible that what is being passed > is actually the address of an element. I find that the above construct is the most easily understandable version. I use an old old 'C' compiler, lattice 2.0 for the PC, at work. For historical reasons, It is not an easy task to maintain the several megs of code that it is my job to maintain and enhance, with any other compiler. Prior coders used compiler dependencies rather heavily. I have *believe it or not * had problems just refering to an array with this compiler and haveing it give me the address. In self defense, I have adopted &(array[0]) as my convention. It does tend to show what the meaning is. Cheers Woody