how to compose the two expression to one?

D

DaVinci

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?
 
E

Eric Sosman

DaVinci said:
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.
 
P

pete

DaVinci said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

DaVinci said:
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.
 
D

Duncan Muirhead

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


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().


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top