Can someone break down this code for me?


#1

[code]int main (int argc, const char * argv[])
{
long secondsSince1970 = time(NULL);
long dateAfter4Million = secondsSince1970 + 4000000;

struct tm calDate;
localtime_r(&dateAfter4Million, &calDate);

printf("The date after 4 million seconds is %d-%d-%d\n", calDate.tm_mon +1 , calDate.tm_mday, calDate.tm_year + 1900);

return 0;

}[/code]

So for struct tm CalDate; we declared a struct and named it CalDate, correct?
now what does this line mean? localtime_r(&dateAfter4Million, &calDate);
and what does the & sign mean?


#2

The & sign means that you are passing the address of your long dateAfter4Million var to the function localtime_r. This behavior was described in the previous chapter “pass by reference”.
localtime_r(&dateAfter4Million, &calDate); is a standard function which takes your passed by reference seconds and packs them into the struct tm which was also passed by reference. Then you are able to use the internal struct functionality to print your date like 01-01-2012 or something similar.

Hope this helps.


#3

Slightly. I still find it confusing. I am rereading all the chapters though before I move to starting Objective C. I think I need to brush up on the vocab a bit more. I get how things are connected, but now always sure why they are.