473,473 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

easy structure question

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
3 2129
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shelly | last post by:
Here is a question that is probably very easy but that I don't know how to do . I have a web site www.aaa.net. A directory structure has been created for me on the server as and it appears to...
0
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size...
2
by: Maximus | last post by:
Hi, Ok my problem is hard to explain... I'm making a directdraw based program and of course maps. I can put texture on my map but I want to save those information in a structure with a x, y and a...
2
by: andy.dreistadt | last post by:
Hi all, I came across another problem that is probably pretty easy but, again, due to my rusty-ness with C, I'm a little stumped. I have a struct that looks like this: /* Instrument Data...
1
by: Ryan Smith | last post by:
I have a code behind ASP.NET app and on one page I populate a user defined structure from a database. I would like to pass that populated structure to another page so I do not need to perform...
10
by: Qwert | last post by:
Hello, is it correct that if a type (System.Type) is a value (.IsValueType=True) and not primitive (.IsPrimitive=False), that type is a structure? Thanks.
0
by: taddub | last post by:
Hi All I need to tell a colleague how to get the structure (table name + column names + primary key + index etc) of each table in a SQL 6.5 database. I can't access the server as it is on...
3
by: Michael | last post by:
Hi, In some code in my header file I have: typedef struct{ int book_id; char title; char author; int year; int price;
2
by: jehugaleahsa | last post by:
Hello: We are running into an issue. Whenever our users save using either the save key or a BindingNavigator, whatever control is in focus won't be updated. Our users are getting confused when...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.