By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

easy structure question

P: n/a
Hi,

I have had a look in the FAQ but cannot see anything related, of course that
doesn't mean it's not there.
In any case:

I have the following:

void function(){
struct tm *time2;
time_t date1;

......stuff......

time2 = localtime(&date2);
year = time2.tm_year;
}

why does the year= time2.tm_year; give me an error that says "request for
member tm_year in somthing not a structure"?

If I replace this line with printf("%s", asctime(time2)); I get a print of
the date I have set in the structure, so I know the structure has valid
data.

Can anyone help me out here?

Regards
Michael
Jul 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael wrote:
Hi,

I have had a look in the FAQ but cannot see anything related, of course that
doesn't mean it's not there.
In any case:

I have the following:

void function(){
struct tm *time2;
time_t date1;

.....stuff......

time2 = localtime(&date2);
year = time2.tm_year;
}

why does the year= time2.tm_year; give me an error that says "request for
member tm_year in somthing not a structure"?

time2 is a pointer to struct tm.

You want time2->tm_year.

--
Ian Collins.
Jul 1 '06 #2

P: n/a
"Michael" <mi*********@yahoo.com> writes:
Hi,

I have had a look in the FAQ but cannot see anything related, of course that
doesn't mean it's not there.
In any case:

I have the following:

void function(){
struct tm *time2; So, time2 is a pointer to a struct tm.
time_t date1;

.....stuff......

time2 = localtime(&date2); localtime can return null. You should test for that just to make sure
there are no problems. You'll have to dereference the pointer next, as
you'll see, and you can't do that if the pointer is null.
I'm not sure if this is in the standard but the pointer returned by
localtime points to a struct tm that's declared static, so make sure
you don't mess things up my interspersing calls to localtime, with
other calls to localtime, gmtime and checking the values returned like
this:

t1=localtime(...);
year=t1.tm_year;
t2=localtime(...);
/* after this call t1 and t2 point to the same thing */
month1=t1->tm_mon+1;
month2=t2->tm_mon+1;
/* month1==month2 */
year = time2.tm_year; Remember, time2 is a pointer to a struct tm. tm_year is a member of a
struct tm structure. time2 is a pointer that's why it says you're
requesting tm_year in something not a structure because time2 is
a pointer to the structure you want. You can dereference the pointer,
so you could use either of these:
1. year = (*time2).tm_year;
2. year = time2->tm_year;
}

why does the year= time2.tm_year; give me an error that says "request for
member tm_year in somthing not a structure"?

If I replace this line with printf("%s", asctime(time2)); I get a print of
the date I have set in the structure, so I know the structure has valid
data.

That's because asctime requires a pointer to struct tm and not just a
struct tm.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 1 '06 #3

P: n/a
Thanks heaps guys

"Nelu" <sp*******@gmail.com> wrote in message
news:b1*************@otaku.freeshell.org...
"Michael" <mi*********@yahoo.com> writes:
Hi,

I have had a look in the FAQ but cannot see anything related, of course
that
doesn't mean it's not there.
In any case:

I have the following:

void function(){
struct tm *time2;

So, time2 is a pointer to a struct tm.
time_t date1;

.....stuff......

time2 = localtime(&date2);

localtime can return null. You should test for that just to make sure
there are no problems. You'll have to dereference the pointer next, as
you'll see, and you can't do that if the pointer is null.
I'm not sure if this is in the standard but the pointer returned by
localtime points to a struct tm that's declared static, so make sure
you don't mess things up my interspersing calls to localtime, with
other calls to localtime, gmtime and checking the values returned like
this:

t1=localtime(...);
year=t1.tm_year;
t2=localtime(...);
/* after this call t1 and t2 point to the same thing */
month1=t1->tm_mon+1;
month2=t2->tm_mon+1;
/* month1==month2 */
year = time2.tm_year;

Remember, time2 is a pointer to a struct tm. tm_year is a member of a
struct tm structure. time2 is a pointer that's why it says you're
requesting tm_year in something not a structure because time2 is
a pointer to the structure you want. You can dereference the pointer,
so you could use either of these:
1. year = (*time2).tm_year;
2. year = time2->tm_year;
}

why does the year= time2.tm_year; give me an error that says "request
for
member tm_year in somthing not a structure"?


If I replace this line with printf("%s", asctime(time2)); I get a print
of
the date I have set in the structure, so I know the structure has valid
data.

That's because asctime requires a pointer to struct tm and not just a
struct tm.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jul 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.