comp.lang.ada
 help / color / mirror / Atom feed
From: fred@genesis.demon.co.uk (Lawrence Kirby)
Subject: Re: Fortran or Ada?
Date: 1998/09/30
Date: 1998-09-30T00:00:00+00:00	[thread overview]
Message-ID: <907198459snz@genesis.demon.co.uk> (raw)
In-Reply-To: Pine.GSO.3.95.980930004116.19512A-100000@holyrood.ed.ac.uk

In article <Pine.GSO.3.95.980930004116.19512A-100000@holyrood.ed.ac.uk>
           sokal@holyrood.ed.ac.uk "Daniel Barker" writes:

>`In the language of everyday life it very often happens that the same word
>signifies in two different ways - and therefore belongs to two different
>symbols - or that two words, which signify in different ways, are
>apparently applied in the same way in the proposition. 

OK.

...

>`If a sign is NOT NECESSARY then it is meaningless. That is the meaning of
>Occam's razor.'

...

>If I may veer into another language, C - and thus justify adding another
>news group to the `to' line -
>
>#include <stdio.h>
>main()
>{
>  int a[100];         /* `a' means array */
>  printf("%p\n", a);  /* `a' means pointer */

%p requires an argument of type void *. Make this

   printf("%p\n", (void *)a);

>  a = a + 1;          /* ILLEGAL, since `a' means array again */

According to the wording of the standard the leftmost a here is converted to
a pointer value that is the address of the first element of a. The reason
this is an error is because this left hand operand is not a modifiable lvalue
(in fact it isn't an lvalue at all, even though a initially is).

>}
>
>`a' here means two different things:

That is an unnecessarily complex interpretation (you mention Occam's razor
above). In all cases here ``a'' refers to the array object defined at the
top of the function.

>(1) an array of 100 normal-width integers;
>(2) a pointer to a normal-width integer (in fact, to the first integer in
>an array of 100 such).

Well, no. a always refers initially to the array. It is just that in some
circumstances it is converted to a pointer to the first element of the array.
The language works in a similar way in other circumstances e.g.

   short i=1, j = 2, k;

   k = i+j;

In this case i and j are both converted to values of type int before
the + operator is applied to theose values. However I hope you won't deny
that i and j refer to variables of type short.

>To say that, in the call to printf(), the first thing is implicitly
>converted to the second thing is to linguistically `work around' the basic
>problem, that `a' has two meanings.

No, it how the standard describes the behaviour of the abstract machine that
it defines. It is certainly possible to describe it in different terms
with the same overall results but this is no more a `work around' than
the alternatives. It is if anything the alternatives that are a workaround
since they take a different approach to the actual definition. 

>Meaning (2) can also be expressed as &a[0]

If you analyse this fully you get the following steps

1. a refers to the defined array object and is an lvalue

2. The expression causes this lvalue to be converted to a pointer to the
   first element of the array.

3. x[y] is equivalent to *(x + y) so a[0] is the same as *(a+0) which reduces
   to *a. Since this operand points to the first element of the array then
   the result of this is an lvalue which designates the first element of a.

4. Unary & takes the address of the object designated by its operand. Since
   the operand is the first element of the array the result is a pointer to
   the first element of the array.

>So, the example reveals two faults in the C language. Namely, that `a', as
>a local variable in one function, can have two meanings;

No, a in main here always refers to the array object declared at the top of
the function. It is an inherent property of C that context can cause the
original value/lvalue to be converted to a value of a different type. This
is best considered as a separate step. Not all contexts do this, The main
ones that don't are &a and sizeof a.

What this shows is that in different contexts different values can be
derived from a. But that is true of any variable. The only difference here
is that there is an implicit conversion from an array to a pointer which
is more radical than most other implicit conversions. C supports another
conversion which is IMO equally radical, if not more so:

    int *ptr = 0;

converts the constant integral expression 0 to a null pointer of type int *.

>and also that one
>of these meanings is may be expressed by either of two symbols. This is a
>crying shame! Why does `a' have two meanings, when, given the possibility
>of `&a[0]', one would have sufficed without reducing the functionality of
>the language?

In many cases it is simpler and more natural to write a instead of &a[0],
e.g.


    int *ptr;


    ptr = a;

There is also 

    ptr = a+1;

where pointer arithmetic is performed on the pointer value derived from a.
This sort of thing has been natural in C since the earliest days. If you
say that it is the assignment or the addition operators that caused the
conversion then you are back to a similar idea of context causing the
conversion that the standard describes.

>The answer is historical, as revealed by an interesting paper, "The
>Development of the C Language" by Dennis Ritchie, available from Dennis
>Ritchie's home page, http://plan9.bell-labs.com/cm/cs/who/dmr/index.html.

Sure it does it because it allows you to express the construct more simply.
One of C's defining features is pointer arithmetic (many other languages have
pointers to allow linked datastructures and dynamic allocation but lack the
ability to perform arithmetic directly on them). Pointer arithmetic is
inherently performed over array objects so it is natural for C to have as
simple a syntax as possible for converting from arrays to pointers. Consider

    puts("Hello, world");

In C string literals are arrays of char. If the language were changed as you
suggest then this would have to be written as

    puts(&"Hello, world"[0]);

(which incidentally is valid in C). Or how about

    char str[10];

    strcpy(&str[0], &"A string"[0]);
    printf(&"The string is '%s'\n"[0], &str[0]);

It would turn C into a much less readable language (even for people who
thought it was unreadable to start with).

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------





  reply	other threads:[~1998-09-30  0:00 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <36068E73.F0398C54@meca.polymtl.ca>
1998-09-22  0:00 ` Fortran or Ada? Gisle S{lensminde
1998-09-22  0:00   ` Jeff Templon
1998-09-22  0:00     ` Rakesh Malhotra
1998-09-22  0:00     ` dewarr
1998-09-23  0:00       ` Jeff Templon
1998-09-23  0:00         ` Corey Minyard
1998-09-24  0:00           ` Joel Seidman
1998-09-24  0:00           ` Craig Burley
1998-09-25  0:00             ` dewarr
1998-09-25  0:00               ` Jonathan Guthrie
1998-09-26  0:00               ` Craig Burley
1998-09-26  0:00                 ` dewarr
1998-09-26  0:00                   ` Craig Burley
1998-09-26  0:00                   ` Robert B. Love 
1998-09-24  0:00         ` Marc A. Criley
1998-09-24  0:00           ` dewarr
1998-09-24  0:00             ` Robert I. Eachus
1998-09-24  0:00               ` William Clodius
1998-09-25  0:00                 ` dewarr
1998-09-25  0:00               ` dewarr
1998-09-25  0:00                 ` Robert I. Eachus
1998-09-25  0:00                   ` dewarr
1998-09-25  0:00             ` jbs
1998-09-26  0:00               ` dewarr
1998-09-28  0:00                 ` jbs
1998-09-29  0:00                   ` William B. Clodius
1998-09-30  0:00                     ` jbs
1998-09-30  0:00                       ` William Clodius
1998-09-30  0:00                       ` dewarr
1998-09-30  0:00                         ` jbs
1998-09-30  0:00                           ` William Clodius
1998-10-03  0:00                       ` Niklas Holsti
1998-10-03  0:00                         ` Toon Moene
1998-10-03  0:00                           ` Niklas Holsti
1998-10-03  0:00                             ` Toon Moene
1998-10-04  0:00                               ` Niklas Holsti
1998-10-05  0:00                                 ` Michel OLAGNON
1998-10-05  0:00                                   ` Ian St. John
1998-10-05  0:00                                     ` Richard D Riehle
1998-10-05  0:00                                       ` dewarr
1998-10-05  0:00                                     ` dewarr
1998-10-05  0:00                                       ` Ian St. John
1998-10-06  0:00                                         ` dewarr
1998-10-06  0:00                                           ` Ian St. John
1998-10-08  0:00                                             ` Offtopic Airport Diversion (Was: Fortran or Ada?) Harold Stevens ** PLEASE SEE SIG **
1998-10-08  0:00                                               ` Ian St. John
1998-10-08  0:00                                             ` Fortran or Ada? Gary L. Scott
1998-10-06  0:00                                           ` Toon Moene
1998-10-06  0:00                                             ` Jonathan Guthrie
1998-10-06  0:00                                           ` James Giles
1998-10-06  0:00                                             ` Mark A Biggar
1998-10-06  0:00                                               ` James Giles
1998-10-07  0:00                                                 ` dewarr
1998-10-07  0:00                                             ` dewarr
1998-10-06  0:00                                     ` Michel OLAGNON
1998-10-07  0:00                                       ` Ian St. John
1998-09-29  0:00                   ` Dean F. Sutherland
1998-09-29  0:00                   ` Dean F. Sutherland
1998-09-29  0:00                   ` Dean F. Sutherland
1998-09-29  0:00                   ` Gautier.DeMontmollin
1998-10-02  0:00                   ` Geoff Bull
1998-10-02  0:00                   ` Robert I. Eachus
1998-10-02  0:00                     ` jbs
1998-10-02  0:00                       ` William Clodius
1998-10-03  0:00                         ` jbs
1998-10-03  0:00                           ` Larry Elmore
1998-10-06  0:00                           ` Robert I. Eachus
1998-10-06  0:00                             ` Pat Rogers
1998-09-24  0:00           ` Jeff Templon
1998-09-25  0:00             ` Dale Stanbrough
1998-09-30  0:00               ` Daniel Barker
1998-09-30  0:00                 ` Lawrence Kirby [this message]
1998-10-01  0:00                   ` Aaron Crane
1998-10-02  0:00                 ` Fortran or Ada or gobbledygook? Wes Groleau
1998-10-05  0:00                 ` Fortran or Ada? Terry Devine
1998-09-25  0:00             ` dewarr
     [not found]           ` <6udre0$ha1$1@nnrp1.dejane <6utg60$h6l$1@nnrp1.dejanews.com>
1998-10-01  0:00             ` robin_v
1998-09-22  0:00     ` Larry Kilgallen
1998-09-22  0:00     ` Corey Minyard
1998-09-23  0:00     ` Frank Ecke
1998-09-23  0:00   ` bglbv
1998-09-23  0:00     ` Robert I. Eachus
1998-09-24  0:00       ` Richard D Riehle
1998-09-23  0:00 Marin David Condic, 561.796.8997, M/S 731-96
  -- strict thread matches above, loose matches on Subject: below --
1998-09-23  0:00 Marin David Condic, 561.796.8997, M/S 731-96
1998-09-23  0:00 ` Gisle S{lensminde
1998-09-24  0:00   ` John McCabe
1998-09-24  0:00     ` Dean F. Sutherland
1998-09-24  0:00     ` Gisle S{lensminde
1998-09-24  0:00 ` Samuel T. Harris
1998-09-24  0:00 Walt Brainerd
1998-09-25  0:00 ` Robert I. Eachus
1998-09-25  0:00 ` dewarr
1998-10-05  0:00 Condic, Marin D.
1998-10-05  0:00 robin
1998-10-05  0:00 Robert I. Eachus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox