easy structure question

M

Michael

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
 
I

Ian Collins

Michael said:
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.
 
N

Nelu

Michael said:
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.
 
M

Michael

Thanks heaps guys

Nelu said:
So, time2 is a pointer to a struct tm.

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 */

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;



That's because asctime requires a pointer to struct tm and not just a
struct tm.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top