[英]Converting string to struct tm in C
I am trying to convert a string to a struct tm. 我正在尝试将字符串转换为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 v245-2%g ++ prog1.c prog1.c:在函数
char* calcage(char**, char**)': prog1.c:143: error: cannot convert
在赋值中将char *' char* calcage(char**, char**)': prog1.c:143: error: cannot convert
为`tm *'
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. 除了我在评论中提出的问题,你所得到的是错误的原因是因为
strptime
返回char *
,而不是struct tm *
,所以要分配birthparse
无效。
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 birth_struct
应该声明为普通结构,而不是结构体的指针,并且您应该将birth_struct
的地址birth_struct
strptime
,例如
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. 如果解析不成功,则从
strptime
返回NULL
,否则返回解析的最后一个字符的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.