<分区>
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* p;
p = (char*)malloc(0);
scanf("%s", p);
printf("%s", p);
free(p);
}
有人可以向我解释为什么我通过终端输入的每个词都会打印给用户吗?我有 malloc(0)
。当我尝试 scanf
时不应该给我一个 segmentation fault
吗?
编辑
为什么这会给我一个编译错误:
p = malloc(sizeof(char) * 2)
当我试图避免 (void*)
error: cannot initialize a variable of type 'char *' with an rvalue of type 'void *'