473,386 Members | 1,908 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,386 software developers and data experts.

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 2127
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.