小编JOS*_*OSI的帖子

malloc放置"垃圾"值

我如何防止或绕过我的变量中的垃圾valus malloc put?附上代码和输出!谢谢!

#include <stdio.h>
#include "stdlib.h"
#include <string.h>

int main() {
    char* hour_char = "13";
    char* day_char = "0";
    char* time = malloc(strlen(hour_char)+strlen(day_char)+2);
    time = strcat(time,day_char);
    time = strcat(time,"-");
    time = strcat(time,hour_char);
    printf("%s",time);
    free(time);
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出:

á[?0-13
Run Code Online (Sandbox Code Playgroud)

c malloc strcat garbage

1
推荐指数
1
解决办法
760
查看次数

标签 统计

c ×1

garbage ×1

malloc ×1

strcat ×1