简体   繁体   中英

Converting string to struct tm in C

I am trying to convert a string to a struct tm. below is my code....i am getting this error when I compile but I am not sure what/how to change the line around so that it works.

v245-2% g++ prog1.c prog1.c: In function char* calcage(char**, char**)': prog1.c:143: error: cannot convert char*' to `tm*' in assignment

char* calcage(char **individual, char **age)
  {
    time_t time_raw_format;
    struct tm * time_struct;
    char *birthday = (char *)malloc(50*sizeof(char));
    struct tm * birthparse;
    struct tm * birth_struct;

    char buf [100];

    time ( &time_raw_format );
    time_struct = localtime ( &time_raw_format );

    strftime (buf,100,"It is: %m/%d/%Y.",time_struct);
    puts (buf);

    printf("person: %s\n", *individual);

    birthday = strrchr(*individual, ',');
    birthday++;

    printf("Birthday:  %s\n", birthday);

   birthparse = strptime(birthday, "%D", birth_struct);
 }

In addition to the issues I raised in the comment, the reason you are getting the error is because strptime returns char * , not struct tm * , so the assignment to birthparse is not valid.

birth_struct should be declared as a normal struct, not a pointer to a struct, and you should give the address of birth_struct to strptime , eg

struct tm birth_struct;

// ...

strptime(birthday, "%D", &birth_struct);

If the parse was unsuccessful, NULL is returned from strptime , otherwise the address of the last character parsed is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM