gpt4 book ai didi

c - 返回 time_t 错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:16 25 4
gpt4 key购买 nike

该程序将返回一组字符串“This is James: 00:00:00”和一个时间格式,但它崩溃了。我相信缺少内存分配,但无法确定错误所在。

FREObject result = 0;

uint32_t len = -1;
const uint8_t *str = 0;
char *temp;
uint8_t *strAll;

time_t curtime;
struct tm *loctime;

/* Get the current time. */
curtime = time(NULL);

/* Convert it to local time representation. */
loctime = localtime(&curtime);

//Turn our actionscrpt code into native code.
if(FREGetObjectAsUTF8(argv[0], &len, &str) == FRE_OK) {
temp = "Hello World! This is ";

strAll = (char *)malloc(sizeof(temp) + sizeof(str) + sizeof(loctime));
strcpy(strAll,temp);
strcat(strAll,str);
strcat(strAll,asctime(loctime));
}

最佳答案

你可能需要 strlen 而不是 sizeof :

strAll = (char *)malloc(sizeof(temp) + sizeof(str) + sizeof(loctime));

sizeof(loctime) 也没什么意义。您可能希望将其替换为 asctime(loctime) 的长度。

也许是这样的:

char *asc = asctime(loctime);
strAll = malloc(strlen(temp) + strlen(str) + stren(asc) + 1);

关于c - 返回 time_t 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8645241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com