Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Difference between these pointer syntaxes Date: Wed, 17 Aug 2016 17:41:40 -0700 Organization: None to speak of Lines: 54 Message-ID: References: <87tweqdeuo.fsf@bsb.me.uk> <87y441a697.fsf@bsb.me.uk> <87r39s8w6c.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="14321"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DtRCWvdrQ7IvAgmFywnZd" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:OqcMjg3NkzE2y0HyonOcfbN2hYk= sha1:btSsbpLsG89eiKaPUCWeQFYfWnM= Xref: csiph.com comp.lang.c:92422 Ian Collins writes: [...] > An interesting thought exercise is to image C where arrays were first > class objects and did not 'decay" to pointers. Would that make for a > better C? Maybe. Would it be a safer C? Probably. Arrays are difficult. In early versions of Pascal, for example, an array of 10 integers and an array of 20 integers were of distinct and incompatible types. There was no way to write a function that could take an argument of either type. I think "conformant arra parameters" were a later addition that would let you do something like that, but as I recall there were a bit ugly. (As you can see I'm fuzzy on the details.) In Ada, arrays with different bounds might be of the same *type* but of different *subtypes*. You can assign an array object to another object, but if the bounds don't match you get a run-time exception. A procedure or function can have a parameter of an *unconstrained array type*, so it can accept an argument of any bounds -- but then the bounds have to be passed implicitly by some unspecified mechanism. (They can be accessed by using attributes, such as Array_Name'Last.) That kind of thing has traditionally been considered "not in the spirit of C". If you changed C by *just* removing the array-to-pointer conversion rule, you'd only make some operations more inconvenient without changing much else. If you additionally permit array expressions to be modifiable lvalues, then you can do array assignments -- but only if the LHS and RHS have the same bounds. For arrays to be first-class objects, I'd at least want to do something like: int obj[10] = { ... }; some_func(obj); and let the body of some_func be able to know the bounds of the parameter. I can't think of a way to add *useful* first-class arrays to C without creating some more or less abstract type, perhaps something similar to C++'s std::vector -- with all that that implies for memory management. (Garbage collection is fine, but I don't think we want to *require* GC as a prerequisite for using arrays.) And of course all the existing mechanisms would have to remain; otherwise we might as well be creating a new language. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"