Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72346

Re: Program to compute elapsed time, learning structures and functions

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Program to compute elapsed time, learning structures and functions
Date 2015-10-09 15:51 -0700
Organization None to speak of
Message-ID <lnio6fejvj.fsf@kst-u.example.com> (permalink)
References (1 earlier) <mv8bku$22a$1@dont-email.me> <d861f166-2cc0-4879-80c3-68bb2759139d@googlegroups.com> <mv8lrv$chl$1@dont-email.me> <ln37xjg3b7.fsf@kst-u.example.com> <mv9b7q$3pk$1@dont-email.me>

Show all headers | View raw


Bartc <bc@freeuk.com> writes:
> On 09/10/2015 22:06, Keith Thompson wrote:
>
>>      struct time result;
>>      // ...
>>      result = {.hour = time_2.hour - time_1.hour,
>>                .minutes = time_2.minutes - time_1.minutes,
>>                .seconds = time_2.seconds - time_1.seconds};
>>
>> The correct version of that code would be:
>>
>>      result = (struct time){.hour = time_2.hour - time_1.hour,
>>                             .minutes = time_2.minutes - time_1.minutes,
>
>> Alla, you mentioned that you thought you could omit the type
>> because it's being assigned to an object of type struct time.
>> The thing to keep in mind is that, in almost all cases, the type
>> of an expression is determined by the expression itself, not by
>> the context in which it appears.  In principle a clever compiler
>> *could* infer the required type for your `{ ... }` based on what it's
>> assigned to -- but it wouldn't be a C compiler.
>
> Why does it need to be clever? It can do a similar thing to what it does 
> for ordinary assignments, or exactly what it already does for 
> initialising using {} and designated initialisers.

For an ordinary assignment, the RHS expression is evaluated in
isolation, without regard to the type of the LHS.  The result is then
converted (if necessary and possible) to the RHS type.

For a hypothetical compound literal with no specified type:

    { .hour = 10, minutes = 20, .seconds = 30 }

the compiler in general has no way of knowing its type.  (That happens
to be a valid initializer for a `struct time` object; the rules for
initializers are different from those for assignments.)  There could
easily be two different incompatible structures with members by those
names.

> It would be interesting to know the rationale behind the need for the 
> pseudo cast, or examples where it really is needed. Needing it because a 
> syntax of a={b} is otherwise confusing would be a rather boring reason 
> though.
>
> True, an assignment is usually followed by an expression, not a left 
> brace, but neither was it followed by a 'parenthesized type name'. And a 
> 'parenthesized type name' denoting a cast was not usually followed by a 
> left brace either. So if changes can be made, why not allow "{" to 
> follow an assignment operator?

An assignment is *always* followed by an expression, and a cast operator
is *never* followed by a left brace.

As I said, currently the type of an expression is almost always
determined by the expression itself, not by the context in which it
appears.  (Exceptions to this are expressions of array and function
types, which are implicitly converted to pointers in some contexts --
but that's still a conversion, not necessarily a change in the way the
expression itself is evaluated.)

I suppose that when compound literals were introduced in C99, the rules
could have been changed so that the parenthesized type name can be
omitted when the compound literal is the RHS of an assignment, an
argument in a function call where the parameter type is known, or the
expression of a return statement.

That's probably the vast majority of the contexts in which compound
literals are used, and off the top of my head I can't think of any real
drawbacks to such a change (quite possibly I'm missing something).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 02:36 -0700
  Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 10:48 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:06 -0700
  Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 11:01 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:07 -0700
  Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 13:26 +0100
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:49 -0700
      Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 16:20 +0100
        Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 09:27 -0700
        Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:29 +0100
        Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 14:06 -0700
          Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 22:24 +0100
            Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 15:51 -0700
      Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 18:26 +0100
        Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:24 +0100
          Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 19:50 +0100
            Re: Program to compute elapsed time, learning structures and functions Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 15:56 -0400
      Re: Program to compute elapsed time, learning structures and functions James Kuyper <jameskuyper@verizon.net> - 2015-10-09 15:31 -0400
      Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-10 06:57 -0700
        Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 17:25 +0100
          Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 19:12 +0100
          Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-10 20:56 +0100
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 21:52 +0100
          Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-10 17:57 -0700
            Re: Program to compute elapsed time, learning structures and functions raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:09 +0000
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-11 11:26 +0100
              Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-11 14:12 -0700
            Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:51 -0700
              Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-15 11:25 -0700
                Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:34 -0700
                Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-16 08:52 -0700
          Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:46 -0700
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-15 19:06 +0100
              Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:24 -0700
          Re: Program to compute elapsed time, learning structures and functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-17 00:10 +0000
      Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 09:52 -0700
  Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:52 -0700
    Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 10:21 -0700
      Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 00:56 -0800
        Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-11-05 01:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 02:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:01 -0800
            Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 03:41 -0800
              Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:55 -0800
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:03 -0800
            Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-11-05 13:27 +0000
              Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:54 -0800
        Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-11-05 12:42 -0800
          Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 15:50 -0800

csiph-web