Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to compose the two expression to one?

Reply
Thread Tools

how to compose the two expression to one?

 
 
DaVinci
Guest
Posts: n/a
 
      04-06-2006
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?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-06-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      04-06-2006
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
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      04-06-2006
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.
 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      04-06-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
No compose key for me? Kilian A. Foth Firefox 0 05-04-2005 12:11 PM
Compose and smileys? Jukka_Mannonen Firefox 10 04-28-2004 03:10 AM



Advertisments