the localtime_s () and the structure and the pointer problems encountered in the use asctime_s

Today, when writing data structures need to use a function of time of the acquisition, the parameters of this function the transfer fee I waited a long time; eventually found the problem on a structure pointer.

struct (TM) { int tm_sec ;         / * sec, range from 0 to 59                * / int tm_min ;         / * min, from 0 to 59                * / int tm_hour ;        / * h, ranging from 0 to 23 is * / int tm_mday ;        / * the first few days of the month, ranging from 1 to 31                    * / int tm_mon ;         / * month, ranging from 0 to 11                * / int tm_year ;        / * since a few years since the 1900                * / int tm_wday
  
  
  
  
  
  
   ;        / * Week of days, ranging from 0 to. 6                * /
   int tm_yday ;        / * day of the year, ranging from 0 to 365 * / int the tm_isdst ;       / * Summer time                        * / } ; // this is the structure of tm
      

/ * The following is the structure definition of a pointer, another definition of the structure directly, compare the two. * /

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main (void) {
 time_t now;
 char S [32];
 struct TimeInfo = (TM) * (* (TM)) the malloc (the sizeof ((TM) )); / * structure pointer timeinfo must be initialized, he's being given a dynamic space, which is the key point, it must be dynamically allocated space or will be error (using uninitialized memory (timeinfo) * /
 
 
 Time (& now); // time () function is to put a positive integer address where the t's, this is a positive integer beginning January 1970 00:00:00 1st (UTC), so far the number of seconds. here is the place into the now
 localtime_s (timeinfo, & now); / * function using the number of seconds localtime now converted to local time tm structure to save, and the address stored in the structure tm which TimeInfo

In this function TimeInfo pointer * /
 asctime_s (S, TimeInfo); / * S is an array of strings previously defined for the character string memory access function asctime tm structure stored in the conversion time * /
 the printf ( " The current date is:% S ", S);
 return 0;
}

------------------------ -------- gorgeous dividing line to a wave

main int (void) {
 time_t now;
 char S [32];
 struct TimeInfo (TM); // herein and is different from the above-defined structure directly rather than a pointer, the system will default initialization
 Time (& now);
 the localtime_s (& TimeInfo, & now); // parentheses TimeInfo must be added to the value character
 asctime_s (S, & timeinfo); // this is added to the value character
 printf ( "current date:% s", S );
 return 0;
}

Guess you like

Origin www.cnblogs.com/ArnoldSchwarzenegger/p/11908229.html