how to compose the two expression to one?

Discussion in 'C Programming' started by DaVinci, Apr 6, 2006.

  1. DaVinci

    DaVinci Guest

    here are two expressions:
    time_t now = time(NULL);
    char* s = ctime(&now);

    how to make them to one expression.

    such as:
    char* s = ctime(&time(NULL)) ,but it is not right.

    what shoulde I do?
     
    DaVinci, Apr 6, 2006
    #1
    1. Advertising

  2. DaVinci

    Eric Sosman Guest

    DaVinci wrote:
    > here are two expressions:
    > time_t now = time(NULL);
    > char* s = ctime(&now);


    Actually, these are not expressions. They are
    variable definitions with initializers; the initializers
    are expressions.

    > how to make them to one expression.
    >
    > such as:
    > char* s = ctime(&time(NULL)) ,but it is not right.


    The argument to ctime() must be a pointer to a time_t
    value. C can only point at stored values ("lvalues"), not
    at the transient values produced by expressions ("rvalues").
    That is, the result of time() is a perfectly legitimate value,
    but you cannot form a pointer to it. You can store it in a
    variable and point at the variable, but you cannot point to
    the value "in flight" before it's stored.

    This implies that the value of time() must be stored
    somewhere before you can create a pointer to it and call
    ctime().

    > what shoulde I do?


    Do what you're doing now. There's nothing wrong with it.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 6, 2006
    #2
    1. Advertising

  3. DaVinci

    pete Guest

    DaVinci wrote:
    >
    > here are two expressions:
    > time_t now = time(NULL);
    > char* s = ctime(&now);


    Those are two object declarations.
    Those are two object definitions.
    Those are two object initializations.

    >
    > how to make them to one expression.
    >
    > such as:
    > char* s = ctime(&time(NULL)) ,but it is not right.
    >
    > what shoulde I do?


    You should forget about it.

    --
    pete
     
    pete, Apr 6, 2006
    #3
  4. DaVinci wrote:
    > here are two expressions:
    > time_t now = time(NULL);
    > char* s = ctime(&now);
    >
    > how to make them to one expression.
    >
    > such as:
    > char* s = ctime(&time(NULL)) ,but it is not right.
    >
    > what shoulde I do?

    You should keep the

    time_t now = time(NULL);
    char* s = ctime(&now);

    as this is clear and concise code.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Apr 6, 2006
    #4
  5. On Thu, 06 Apr 2006 08:18:28 -0400, Eric Sosman wrote:

    > DaVinci wrote:
    >> here are two expressions:
    >> time_t now = time(NULL);
    >> char* s = ctime(&now);

    >
    > Actually, these are not expressions. They are
    > variable definitions with initializers; the initializers
    > are expressions.
    >
    >> how to make them to one expression.
    >>
    >> such as:
    >> char* s = ctime(&time(NULL)) ,but it is not right.

    >
    > The argument to ctime() must be a pointer to a time_t
    > value. C can only point at stored values ("lvalues"), not
    > at the transient values produced by expressions ("rvalues").
    > That is, the result of time() is a perfectly legitimate value,
    > but you cannot form a pointer to it. You can store it in a
    > variable and point at the variable, but you cannot point to
    > the value "in flight" before it's stored.
    >
    > This implies that the value of time() must be stored
    > somewhere before you can create a pointer to it and call
    > ctime().
    >
    >> what shoulde I do?

    >
    > Do what you're doing now. There's nothing wrong with it.


    I don't think you can combine the two variable declarations, but you could
    combine the expressions using the comma operator:
    s = (now=time(NULL), ctime( &now));
    However, like the other posters, I'd say don't.
    Duncan
     
    Duncan Muirhead, Apr 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. [XaToA]

    compose date

    [XaToA], Aug 8, 2003, in forum: Java
    Replies:
    1
    Views:
    419
    Roedy Green
    Aug 8, 2003
  2. Replies:
    2
    Views:
    1,637
    Adrienne Boswell
    Jul 28, 2006
  3. aspineux
    Replies:
    0
    Views:
    244
    aspineux
    Sep 16, 2011
  4. Jochen Scheire

    compose variables

    Jochen Scheire, Oct 13, 2004, in forum: ASP General
    Replies:
    1
    Views:
    128
    Ray Costanzo [MVP]
    Oct 13, 2004
  5. Replies:
    0
    Views:
    83
Loading...

Share This Page