当我尝试使用带有字符串指针的 malloc 进行扫描时,它给出了一个段错误
main(){
char *a;
a = (char)malloc(50);
printf("enter a string\t");
scanf("%s", a);
printf("%s\n", a);
}
最佳答案
a = (char)malloc(50);
在这里,您打算将其类型转换为 char *
而不是 char
。
注意最好不要强制转换malloc
的返回类型。参见 Do I cast the result of malloc?
关于c - 我在将 malloc 与字符串指针一起使用时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632329/