我是 C 编程的初学者,并试图通过 c 代码使用系统日期获取昨天的日期,并像这样昨天Date_dtmmddyy 附加到字符串“yesterdayDate_dt”中,但面临运行时错误“总线错误 10”。
我的代码如下
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
int main(void)
{
time_t now = time(NULL);
struct tm *t = localtime(&now);
int dInt = t->tm_mday+1;
int mInt = t->tm_mon -1;
int yInt = t->tm_year+1900;
char *date= "23";
char *month = "01";
char *year = "13";
sprintf(date, "%d", dInt);
sprintf(month, "%d", mInt);
char *yestDt = (char *)malloc(strlen(date)+strlen(month)+strlen(year)+1);
strcpy(str,month);
strcat(str,date);
strcat(str,year);
printf("str:%s",yestDt);
return 0;
}