分段故障而来自用户采取串 [英] Segmentation fault while taking string from user
本文介绍了分段故障而来自用户采取串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
code:
int main()
{
char *name=NULL;
int n;
printf("\nenter the string\n");
scanf("%s",name);
n=strlen(name);
printf("%d",n);
return 0;
}
我收到分段错误。什么错与code?我已经包括 stdio.h中
,文件stdlib.h
,文件string.h
。
推荐答案
您没有分配的字符指针名称
的内存。
You didn't allocate any memory for pointer to char name
.
例如:
char * name = malloc( sizeof( char ) * MAX_STRING_LENGTH ) ;
这篇关于分段故障而来自用户采取串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文