473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to compose the two expression to one?

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?

Apr 6 '06 #1
4 1179
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
es*****@acm-dot-org.invalid
Apr 6 '06 #2
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
Apr 6 '06 #3
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.
Apr 6 '06 #4
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
Apr 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
1
by: Jochen Scheire | last post by:
Hello, I have a list with a lot of variables. The variables all start with Langstring, followed by a number. Langstring1 = "text1" Langstring2 = "text2"... I have a database with only...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: MaxH | last post by:
I need to perform programmatically from a C# .net application the following tasks: _ open an outlook (or default e-mail client) compose message window; _ attach a document to the new message; _...
1
by: nader | last post by:
hello what is the code to creating text compose many cells almost like excel program to or how do that and thankyou for all :?: Posted Via Usenet.com Premium Usenet Newsgroup Services...
14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
3
by: diSangro | last post by:
Hello community, I have a stupid question...of course Im not expert of csh scripting I couldn't get how to compose a string in a csh script. suppose I have: set STR1 = "text1" set STR2 =...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.